Skip to content

Commit cbb6dc1

Browse files
Split yggdrasilctl code into separate functions (refactoring) (yggdrasil-network#815)
* Move yggdrasilctl responses to separate functions * Move yggdrasilctl request switch to separate function * Add empty lines * Create struct CmdLine for yggdrasilctl * Move yggdrasilctl command line parsing to separate func * Turn struct CmdLine into CmdLineEnv * Rename func parseCmdLine to parseFlagsAndArgs * Move yggdrasilctl endpoint setting logic into separate func * Function to create yggdrasilctl CmdLineEnv * Reorder code * Move struct fields into lines * Turn yggdrasilctl CmdLineEnv funcs to methods * Move yggdrasilctl connection code to separate func * Rename functions * Move yggdrasilctl command line env to separate mod * Move yggdrasilctl command line env to main mod * Run goimports Co-authored-by: Neil Alexander <[email protected]>
1 parent b333c7d commit cbb6dc1

File tree

2 files changed

+408
-309
lines changed

2 files changed

+408
-309
lines changed

cmd/yggdrasilctl/cmd_line_env.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"fmt"
7+
"io/ioutil"
8+
"log"
9+
"os"
10+
11+
"github.com/hjson/hjson-go"
12+
"golang.org/x/text/encoding/unicode"
13+
14+
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
15+
)
16+
17+
type CmdLineEnv struct {
18+
args []string
19+
endpoint, server string
20+
injson, verbose, ver bool
21+
}
22+
23+
func newCmdLineEnv() CmdLineEnv {
24+
var cmdLineEnv CmdLineEnv
25+
cmdLineEnv.endpoint = defaults.GetDefaults().DefaultAdminListen
26+
return cmdLineEnv
27+
}
28+
29+
func (cmdLineEnv *CmdLineEnv) parseFlagsAndArgs() {
30+
flag.Usage = func() {
31+
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] command [key=value] [key=value] ...\n\n", os.Args[0])
32+
fmt.Println("Options:")
33+
flag.PrintDefaults()
34+
fmt.Println()
35+
fmt.Println("Please note that options must always specified BEFORE the command\non the command line or they will be ignored.")
36+
fmt.Println()
37+
fmt.Println("Commands:\n - Use \"list\" for a list of available commands")
38+
fmt.Println()
39+
fmt.Println("Examples:")
40+
fmt.Println(" - ", os.Args[0], "list")
41+
fmt.Println(" - ", os.Args[0], "getPeers")
42+
fmt.Println(" - ", os.Args[0], "-v getSelf")
43+
fmt.Println(" - ", os.Args[0], "setTunTap name=auto mtu=1500 tap_mode=false")
44+
fmt.Println(" - ", os.Args[0], "-endpoint=tcp://localhost:9001 getDHT")
45+
fmt.Println(" - ", os.Args[0], "-endpoint=unix:///var/run/ygg.sock getDHT")
46+
}
47+
48+
server := flag.String("endpoint", cmdLineEnv.endpoint, "Admin socket endpoint")
49+
injson := flag.Bool("json", false, "Output in JSON format (as opposed to pretty-print)")
50+
verbose := flag.Bool("v", false, "Verbose output (includes public keys)")
51+
ver := flag.Bool("version", false, "Prints the version of this build")
52+
53+
flag.Parse()
54+
55+
cmdLineEnv.args = flag.Args()
56+
cmdLineEnv.server = *server
57+
cmdLineEnv.injson = *injson
58+
cmdLineEnv.verbose = *verbose
59+
cmdLineEnv.ver = *ver
60+
}
61+
62+
func (cmdLineEnv *CmdLineEnv) setEndpoint(logger *log.Logger) {
63+
if cmdLineEnv.server == cmdLineEnv.endpoint {
64+
if config, err := ioutil.ReadFile(defaults.GetDefaults().DefaultConfigFile); err == nil {
65+
if bytes.Equal(config[0:2], []byte{0xFF, 0xFE}) ||
66+
bytes.Equal(config[0:2], []byte{0xFE, 0xFF}) {
67+
utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
68+
decoder := utf.NewDecoder()
69+
config, err = decoder.Bytes(config)
70+
if err != nil {
71+
panic(err)
72+
}
73+
}
74+
var dat map[string]interface{}
75+
if err := hjson.Unmarshal(config, &dat); err != nil {
76+
panic(err)
77+
}
78+
if ep, ok := dat["AdminListen"].(string); ok && (ep != "none" && ep != "") {
79+
cmdLineEnv.endpoint = ep
80+
logger.Println("Found platform default config file", defaults.GetDefaults().DefaultConfigFile)
81+
logger.Println("Using endpoint", cmdLineEnv.endpoint, "from AdminListen")
82+
} else {
83+
logger.Println("Configuration file doesn't contain appropriate AdminListen option")
84+
logger.Println("Falling back to platform default", defaults.GetDefaults().DefaultAdminListen)
85+
}
86+
} else {
87+
logger.Println("Can't open config file from default location", defaults.GetDefaults().DefaultConfigFile)
88+
logger.Println("Falling back to platform default", defaults.GetDefaults().DefaultAdminListen)
89+
}
90+
} else {
91+
cmdLineEnv.endpoint = cmdLineEnv.server
92+
logger.Println("Using endpoint", cmdLineEnv.endpoint, "from command line")
93+
}
94+
}

0 commit comments

Comments
 (0)