diff --git a/argParser.go b/argParser.go index a280dff..aba2ac7 100644 --- a/argParser.go +++ b/argParser.go @@ -10,21 +10,22 @@ import ( ) var colors = []string{"DefaultColor", "Black", "DarkRed", "DarkGreen", "Brown", "DarkBlue", "Purple", "Cyan", - "LightGray", "DarkGray", "Red", "Green", "Yellow", "Blue", "Fuchsia", "Turquoise", "White",} - -type Arguments struct{ - Address *string - Port *string - Command *string - ScriptFile *string - Silent *bool - TextColor prompt.Color - PromptColor prompt.Color - PreviewColor prompt.Color - SuggestionColor prompt.Color + "LightGray", "DarkGray", "Red", "Green", "Yellow", "Blue", "Fuchsia", "Turquoise", "White"} + +type Arguments struct { + Address *string + Port *string + Command *string + ScriptFile *string + Silent *bool + Version *bool + TextColor prompt.Color + PromptColor prompt.Color + PreviewColor prompt.Color + SuggestionColor prompt.Color SuggestionBgColor prompt.Color - SelectedColor prompt.Color - SelectedBgColor prompt.Color + SelectedColor prompt.Color + SelectedBgColor prompt.Color } func ParseArgs() Arguments { @@ -37,34 +38,36 @@ Arguments allow sending single commands or scripts from files non-interactively. Help: "The network address of the instrument. If not provided, Sclipi will use your network information and auto-completion to assist you"}) args.Port = parser.String("p", "port", &argparse.Options{ Default: "5025", - Help: "The SCPI port of the instrument"}) + Help: "The SCPI port of the instrument"}) args.Command = parser.String("c", "command", &argparse.Options{ Help: "A single SCPI command to send non-interactively. Must set address if using this feature"}) args.ScriptFile = parser.String("f", "file", &argparse.Options{ Help: "The path to a newline-delimited list of commands to be run non-interactively. Must set address if using this feature"}) args.Silent = parser.Flag("s", "silent", &argparse.Options{ Help: "Suppresses unnecessary output"}) + args.Version = parser.Flag("", "version", &argparse.Options{ + Help: "Print version information"}) textColorFlag := parser.Selector("", "text-color", colors, &argparse.Options{ Default: colors[prompt.Yellow], - Help: "The command line text color"}) + Help: "The command line text color"}) promptColorFlag := parser.Selector("", "prompt-color", colors, &argparse.Options{ Default: colors[prompt.Blue], - Help: "The command line text color"}) + Help: "The command line text color"}) previewColorFlag := parser.Selector("", "preview-color", colors, &argparse.Options{ Default: colors[prompt.Blue], - Help: "The preview text color"}) + Help: "The preview text color"}) suggestionColorFlag := parser.Selector("", "suggestion-color", colors, &argparse.Options{ Default: colors[prompt.White], - Help: "The suggestion text color"}) + Help: "The suggestion text color"}) suggestionBgColorFlag := parser.Selector("", "suggestion-bg-color", colors, &argparse.Options{ Default: colors[prompt.DarkBlue], - Help: "The suggestion bg color"}) + Help: "The suggestion bg color"}) selectedColorFlag := parser.Selector("", "selected-color", colors, &argparse.Options{ Default: colors[prompt.Black], - Help: "The selected text color"}) + Help: "The selected text color"}) selectedBgColorFlag := parser.Selector("", "selected-bg-color", colors, &argparse.Options{ Default: colors[prompt.Cyan], - Help: "The selected bg color"}) + Help: "The selected bg color"}) parser.HelpFunc = HelpMessage @@ -130,7 +133,7 @@ func HelpMessage(o *argparse.Command, _ interface{}) string { temp := "Color Options: " temp += strings.Repeat(" ", argPadding-len(temp)) for _, color := range colors { - temp += "\"" + color + "\", " + temp += "\"" + color + "\", " } temp = strings.TrimSuffix(temp, ", ") result = addToLastLine(result, temp, maxWidth, argPadding, true) @@ -171,25 +174,43 @@ func getLastLine(input string) string { } func colorFromString(color string) prompt.Color { - switch color{ - case "DefaultColor": return prompt.DefaultColor - case "Black": return prompt.Black - case "DarkRed": return prompt.DarkRed - case "DarkGreen": return prompt.DarkGreen - case "Brown": return prompt.Brown - case "DarkBlue": return prompt.DarkBlue - case "Purple": return prompt.Purple - case "Cyan": return prompt.Cyan - case "LightGray": return prompt.LightGray - case "DarkGray": return prompt.DarkGray - case "Red": return prompt.Red - case "Green": return prompt.Green - case "Yellow": return prompt.Yellow - case "Blue": return prompt.Blue - case "Fuchsia": return prompt.Fuchsia - case "Turquoise": return prompt.Turquoise - case "White": return prompt.White - default: log.Fatal("Color not found: " + color) + switch color { + case "DefaultColor": + return prompt.DefaultColor + case "Black": + return prompt.Black + case "DarkRed": + return prompt.DarkRed + case "DarkGreen": + return prompt.DarkGreen + case "Brown": + return prompt.Brown + case "DarkBlue": + return prompt.DarkBlue + case "Purple": + return prompt.Purple + case "Cyan": + return prompt.Cyan + case "LightGray": + return prompt.LightGray + case "DarkGray": + return prompt.DarkGray + case "Red": + return prompt.Red + case "Green": + return prompt.Green + case "Yellow": + return prompt.Yellow + case "Blue": + return prompt.Blue + case "Fuchsia": + return prompt.Fuchsia + case "Turquoise": + return prompt.Turquoise + case "White": + return prompt.White + default: + log.Fatal("Color not found: " + color) } return prompt.DefaultColor } diff --git a/go.mod b/go.mod index 1a05558..a058451 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/go-ole/go-ole v1.2.4 // indirect github.com/google/gops v0.3.6 // indirect github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19 // indirect + github.com/magefile/mage v1.9.0 github.com/mattn/go-tty v0.0.3 // indirect github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 // indirect github.com/schollz/progressbar v1.0.0 diff --git a/go.sum b/go.sum index 13dcbcd..55371f3 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/magefile/mage v1.9.0 h1:t3AU2wNwehMCW97vuqQLtw6puppWXHO+O2MHo5a50XE= +github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= diff --git a/magefile.go b/magefile.go new file mode 100644 index 0000000..6693ce9 --- /dev/null +++ b/magefile.go @@ -0,0 +1,35 @@ +// +build mage + +package main + +import ( + "github.com/magefile/mage/sh" +) + +var Default = Install + +func Build() error { + return sh.Run("go", "build", ".") +} + +func BuildWindows() error { + env := map[string]string{"GOOS": "windows", "GOARCH": "amd64"} + return sh.RunWith(env, "go", "build", ".") +} + +func Install() error { + version, err := sh.Output("git", "describe", "--always", "--long", "--dirty") + if err != nil { + return err + } + return sh.Run("go", "install", "-v", "-ldflags", "-X main.version="+version, ".") +} + +func Test() error { + return sh.RunV("go", "test", "-v") +} + +func Clean() { + sh.Rm("sclipi") + sh.Rm("sclipi.exe") +} diff --git a/main.go b/main.go index 9d0ca52..28dfddf 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,16 @@ import ( "time" ) +var version = "undefined" + func main() { args := ParseArgs() + if *args.Version { + fmt.Println(version) + return + } + if *args.Command != "" { runCommand(*args.Command, *args.Address, *args.Port) return @@ -66,7 +73,9 @@ func main() { } func printIntroText(silent bool) { - if silent {return} + if silent { + return + } fmt.Println("Welcome to the SCPI cli!") fmt.Println("Use Tab to navigate auto-completion options") fmt.Println("Use `CTRL-D`, `quit`, or `exit` to exit this program") @@ -81,7 +90,9 @@ func printHelp() { } func getAddress(args Arguments) string { - if *args.Address != "" { return *args.Address } + if *args.Address != "" { + return *args.Address + } ic := ipCompleter{} var result string for { @@ -117,7 +128,7 @@ func buildAndConnectInstrument(address string, port string) (instrument, error) inst = &scpiInstrument{} } - if err := inst.Connect(5 * time.Second, address + ":" + port); err != nil { + if err := inst.Connect(5*time.Second, address+":"+port); err != nil { return inst, err } @@ -125,13 +136,15 @@ func buildAndConnectInstrument(address string, port string) (instrument, error) } type Progress struct { - Silent bool - bar *progressbar.ProgressBar + Silent bool + bar *progressbar.ProgressBar initialized bool } func (p *Progress) Forward(percent int) { - if p.Silent { return } + if p.Silent { + return + } if !p.initialized { p.bar = progressbar.New(100) p.initialized = true