initialize read config utility

This commit is contained in:
Baipyrus 2024-08-30 18:08:24 +02:00
parent cc133304dd
commit d2162aac5c
3 changed files with 45 additions and 1 deletions

View File

@ -1,11 +1,13 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/Baipyrus/ProxySwitcher/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -14,7 +16,9 @@ var setCmd = &cobra.Command{
Use: "set", Use: "set",
Short: "Enable the current internet proxy settings", Short: "Enable the current internet proxy settings",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Setting Proxy Settings...\n") config, _ := util.ReadConfigs()
data, _ := json.Marshal(config)
fmt.Printf("%s\n\n", string(data))
// Block process until interrupted // Block process until interrupted
done := make(chan os.Signal, 1) done := make(chan os.Signal, 1)

26
util/config.go Normal file
View File

@ -0,0 +1,26 @@
package util
import (
"encoding/json"
"io"
"os"
)
func ReadConfigs() ([]Config, error) {
file, readErr := os.Open("configs.json")
if readErr != nil {
return nil, readErr
}
defer file.Close()
bytes, _ := io.ReadAll(file)
var config []Config
unmarshalErr := json.Unmarshal(bytes, &config)
if unmarshalErr != nil {
return nil, unmarshalErr
}
return config, nil
}

14
util/types.go Normal file
View File

@ -0,0 +1,14 @@
package util
type Config struct {
Name string `json:"name"`
Cmd string `json:"cmd"`
Set []Variant `json:"set,omitempty"`
Unset []Variant `json:"unset,omitempty"`
}
type Variant struct {
Arguments []string `json:"args"`
Type string `json:"type,omitempty"`
Equator string `json:"equator,omitempty"`
}