Skip to content

Commit

Permalink
Added magefile. Added version target for build flags and associated a…
Browse files Browse the repository at this point in the history
…rgument
  • Loading branch information
Bradley Hutchinson committed Nov 25, 2019
1 parent 83b7ef9 commit e56a60d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 48 deletions.
105 changes: 63 additions & 42 deletions argParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
35 changes: 35 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
@@ -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")
}
25 changes: 19 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -117,21 +128,23 @@ 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
}

return inst, nil
}

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
Expand Down

0 comments on commit e56a60d

Please sign in to comment.