From d2162aac5c23cc3f49f73a552c803c98b032c93b Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 30 Aug 2024 18:08:24 +0200 Subject: [PATCH] initialize read config utility --- cmd/set.go | 6 +++++- util/config.go | 26 ++++++++++++++++++++++++++ util/types.go | 14 ++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 util/config.go create mode 100644 util/types.go diff --git a/cmd/set.go b/cmd/set.go index 68865bf..27b1324 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -1,11 +1,13 @@ package cmd import ( + "encoding/json" "fmt" "os" "os/signal" "syscall" + "github.com/Baipyrus/ProxySwitcher/util" "github.com/spf13/cobra" ) @@ -14,7 +16,9 @@ var setCmd = &cobra.Command{ Use: "set", Short: "Enable the current internet proxy settings", 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 done := make(chan os.Signal, 1) diff --git a/util/config.go b/util/config.go new file mode 100644 index 0000000..a7788b9 --- /dev/null +++ b/util/config.go @@ -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 +} diff --git a/util/types.go b/util/types.go new file mode 100644 index 0000000..4173215 --- /dev/null +++ b/util/types.go @@ -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"` +}