Merge pull request #20 from Baipyrus/better-config-files

Config Directory With Examples Instead Of Single File
This commit is contained in:
Baipyrus 2024-09-24 15:48:01 +02:00 committed by GitHub
commit 2a21810a99
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 201 additions and 136 deletions

View File

@ -10,7 +10,7 @@ var debugCmd = &cobra.Command{
Use: "debug",
Short: "Output all parsed configurations for debugging",
Run: func(cmd *cobra.Command, args []string) {
proxy.Debug(cfgFile)
proxy.Debug(cfgPath)
},
}

View File

@ -7,7 +7,7 @@ import (
)
var (
cfgFile string
cfgPath string
// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
@ -33,7 +33,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVarP(&cfgFile, "configs", "c", "configs.json", "configurations file")
rootCmd.PersistentFlags().StringVarP(&cfgPath, "configs", "c", "configs/", "configurations path")
// Cobra also supports local flags, which will only run
// when this action is called directly.

View File

@ -37,8 +37,9 @@ var saveCmd = &cobra.Command{
var input string
fmt.Print("Save this data? (Y/n) ")
fmt.Scanln(&input)
if input == "" || strings.ToLower(input) == "y" {
util.SaveConfig(cfgFile, config)
util.SaveConfig(cfgPath, config)
}
},
}

View File

@ -10,7 +10,7 @@ var setCmd = &cobra.Command{
Use: "set",
Short: "Enable the current internet proxy settings",
Run: func(cmd *cobra.Command, args []string) {
proxy.Set(cfgFile)
proxy.Set(cfgPath)
},
}

View File

@ -10,7 +10,7 @@ var unsetCmd = &cobra.Command{
Use: "unset",
Short: "Disable the current internet proxy settings",
Run: func(cmd *cobra.Command, args []string) {
proxy.Unset(cfgFile)
proxy.Unset(cfgPath)
},
}

View File

@ -1,104 +0,0 @@
[
{
"name": "env",
"cmd": "[System.Environment]::SetEnvironmentVariable('$PRSW_ARG', $PRSW_ARG, 'User')",
"set": [
{
"args": [
"http_proxy"
],
"surround": "\"",
"type": "variable"
}, {
"args": [
"https_proxy"
],
"surround": "\"",
"type": "variable"
}
],
"unset": [
{
"args": [
"http_proxy",
"[NullString]::Value"
],
"type": "variable"
}, {
"args": [
"https_proxy",
"[NullString]::Value"
],
"type": "variable"
}
]
}, {
"name": "git",
"set": [
{
"args": [
"config",
"--global",
"http.proxy"
]
}, {
"args": [
"config",
"--global",
"https.proxy"
]
}
],
"unset": [
{
"args": [
"config",
"--global",
"--unset",
"http.proxy"
]
}, {
"args": [
"config",
"--global",
"--unset",
"https.proxy"
]
}
]
}, {
"name": "npm",
"set": [
{
"args": [
"config",
"set",
"proxy"
],
"equator": "="
}, {
"args": [
"config",
"set",
"https-proxy"
],
"equator": "="
}
],
"unset": [
{
"args": [
"config",
"delete",
"proxy"
]
}, {
"args": [
"config",
"delete",
"https-proxy"
]
}
]
}
]

34
configs/env.json Normal file
View File

@ -0,0 +1,34 @@
{
"name": "env",
"cmd": "[System.Environment]::SetEnvironmentVariable('$PRSW_ARG', $PRSW_ARG, 'User')",
"set": [
{
"args": [
"http_proxy"
],
"surround": "\"",
"type": "variable"
}, {
"args": [
"https_proxy"
],
"surround": "\"",
"type": "variable"
}
],
"unset": [
{
"args": [
"http_proxy",
"[NullString]::Value"
],
"type": "variable"
}, {
"args": [
"https_proxy",
"[NullString]::Value"
],
"type": "variable"
}
]
}

35
configs/git.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "git",
"set": [
{
"args": [
"config",
"--global",
"http.proxy"
]
}, {
"args": [
"config",
"--global",
"https.proxy"
]
}
],
"unset": [
{
"args": [
"config",
"--global",
"--unset",
"http.proxy"
]
}, {
"args": [
"config",
"--global",
"--unset",
"https.proxy"
]
}
]
}

35
configs/npm.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "npm",
"set": [
{
"args": [
"config",
"set",
"proxy"
],
"equator": "="
}, {
"args": [
"config",
"set",
"https-proxy"
],
"equator": "="
}
],
"unset": [
{
"args": [
"config",
"delete",
"proxy"
]
}, {
"args": [
"config",
"delete",
"https-proxy"
]
}
]
}

21
configs/pip.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "pip",
"set": [
{
"args": [
"config",
"set",
"global.proxy"
]
}
],
"unset": [
{
"args": [
"config",
"unset",
"global.proxy"
]
}
]
}

View File

@ -0,0 +1,21 @@
{
"name": "SOCKS",
"cmd": "[void] 'Toggle SOCKS v5'; $PRSW_ARG",
"set": [
{
"args": [
"Start-Process -FilePath 'cmd.exe' -ArgumentList '/c', 'start', 'ssh', '-D', 'PORT', '-q', '-C', '-N', '-f', 'HOST'"
],
"type": "variable",
"discard": true
}
],
"unset": [
{
"args": [
"TASKKILL /F /PID ((Get-NetTCPConnection -LocalPort 1337).OwningProcess | Select -First 1)"
],
"type": "variable"
}
]
}

View File

@ -22,8 +22,8 @@ func mapCmdsToStr(commands []*util.Command) string {
return strings.Join(output, "\n")
}
func Debug(cfgFile string) {
path, _ := filepath.Abs(cfgFile)
func Debug(cfgPath string) {
path, _ := filepath.Abs(cfgPath)
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
path = "[N/A]"
}
@ -41,7 +41,7 @@ func Debug(cfgFile string) {
fmt.Printf("Enabled: %t\n", proxy.Enabled)
fmt.Printf("Server: %s\n\n", proxyServer)
configs, _ := util.ReadConfigs(cfgFile)
configs, _ := util.ReadConfigs(cfgPath)
for _, config := range configs {
configCmd := config.Name
// Use command instead of name, if given

View File

@ -4,7 +4,7 @@ import (
"github.com/Baipyrus/ProxySwitcher/util"
)
func Set(cfgFile string) {
func Set(cfgPath string) {
stdin, closeFunc, _ := util.ReadyCmd()
proxy, _ := ReadSystemProxy()
@ -13,7 +13,7 @@ func Set(cfgFile string) {
SetSystemProxy(true)
}
configs, _ := util.ReadConfigs(cfgFile)
configs, _ := util.ReadConfigs(cfgPath)
for _, config := range configs {
configCmd := config.Name
// Use command instead of name, if given

View File

@ -4,7 +4,7 @@ import (
"github.com/Baipyrus/ProxySwitcher/util"
)
func Unset(cfgFile string) {
func Unset(cfgPath string) {
stdin, closeFunc, _ := util.ReadyCmd()
proxy, _ := ReadSystemProxy()
@ -13,7 +13,7 @@ func Unset(cfgFile string) {
SetSystemProxy(false)
}
configs, _ := util.ReadConfigs(cfgFile)
configs, _ := util.ReadConfigs(cfgPath)
for _, config := range configs {
configCmd := config.Name
// Use command instead of name, if given

View File

@ -2,38 +2,60 @@ package util
import (
"encoding/json"
"fmt"
"io/fs"
"path/filepath"
"strings"
"io"
"os"
)
func ReadConfigs(name string) ([]*Config, error) {
file, err := os.Open(name)
func ReadConfigs(cfgPath string) ([]*Config, error) {
var configs []*Config
if err != nil {
return nil, err
}
defer file.Close()
err := filepath.Walk(cfgPath, func(path string, info fs.FileInfo, err error) error {
name := info.Name()
isExample := strings.HasSuffix(name, ".example.json")
notJson := !strings.HasSuffix(name, ".json")
if info.IsDir() || isExample || notJson {
return nil
}
bytes, _ := io.ReadAll(file)
file, err := os.Open(path)
var config []*Config
err = json.Unmarshal(bytes, &config)
if err != nil {
return nil
}
defer file.Close()
if err != nil {
return nil, err
}
return config, nil
bytes, err := io.ReadAll(file)
if err != nil {
return nil
}
var config *Config
err = json.Unmarshal(bytes, &config)
if err != nil {
return nil
}
configs = append(configs, config)
return nil
})
return configs, err
}
func SaveConfig(name string, config Config) error {
configs, _ := ReadConfigs(name)
configs = append(configs, &config)
data, err := json.Marshal(configs)
func SaveConfig(cfgPath string, config Config) error {
data, err := json.Marshal(config)
if err != nil {
return err
}
err = os.WriteFile(name, data, 0666)
cfgName := fmt.Sprintf("%s.json", config.Name)
cfgFile := filepath.Join(cfgPath, cfgName)
err = os.WriteFile(cfgFile, data, 0666)
return err
}