Goodbye pflag... Welcome kingpin...

Rehauled the command-line interface with kingpin.v2
This commit is contained in:
Elia el Lazkani 2020-02-26 01:01:35 +01:00
parent 49cd6008b4
commit 724ffee8e4

486
main.go
View file

@ -9,9 +9,15 @@ import (
"os"
"strings"
flag "github.com/spf13/pflag"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
// Version
const VERSION = "0.0.3"
// Author
const AUTHOR = "Elia el Lazkani"
// Create a list of constants to figure out
// the command line flags and translate them
// into uri flags.
@ -30,7 +36,6 @@ const (
NOCOLORS
ADDFRAME
MIDTRANSPARENCY
TRANSPARENCY
LOCATION
LANGUAGE
PNG
@ -56,243 +61,6 @@ const (
MidTransparencySwitch = "T"
)
// Define a command parameters struct.
// This will hold all the configurations
// set by the command line parameters provided.
type cmdParams struct {
Flags int
TransparencyLevel int
Location string
Language string
}
// Create a method to figure out the command parameters
// provided and translate them into uri parameters.
// This will figure out if we need to download the PNG image
// as well.
func (cmdP *cmdParams) generateParamFormat() (string, bool, error) {
var params []string
var prefix []string
var download bool
prefix = append(prefix, "?")
if cmdP.Flags&METRIC == METRIC {
params = append(params, MetricSwitch)
}
if cmdP.Flags&USCS == USCS {
params = append(params, UscsSwitch)
}
if cmdP.Flags&METERSECOND == METERSECOND {
params = append(params, MeterSecondSwitch)
}
if cmdP.Flags&ZERO == ZERO {
params = append(params, ZeroSwitch)
}
if cmdP.Flags&ONE == ONE {
params = append(params, OneSwitch)
}
if cmdP.Flags&TWO == TWO {
params = append(params, TwoSwitch)
}
if cmdP.Flags&IGNOREUSERAGENT == IGNOREUSERAGENT {
params = append(params, IgnoreUserAgentSwitch)
}
if cmdP.Flags&FOLLOWLINK == FOLLOWLINK {
params = append(params, FollowLinkSwitch)
}
if cmdP.Flags&NARROW == NARROW {
params = append(params, NarrowSwitch)
}
if cmdP.Flags&QUIET == QUIET {
params = append(params, QuietSwitch)
}
if cmdP.Flags&SUPERQUIET == SUPERQUIET {
params = append(params, SuperQuietSwitch)
}
if cmdP.Flags&NOCOLORS == NOCOLORS {
params = append(params, NoColorsSwitch)
}
if cmdP.Flags&ADDFRAME == ADDFRAME {
params = append(params, AddFrameSwitch)
}
if cmdP.Flags&MIDTRANSPARENCY == MIDTRANSPARENCY {
params = append(params, MidTransparencySwitch)
}
if cmdP.Flags&PNG == PNG {
prefix[0] = "_"
if cmdP.Flags&TRANSPARENCY == TRANSPARENCY && cmdP.TransparencyLevel >= 0 && cmdP.TransparencyLevel <= 100 {
params = append(params, strings.Join([]string{"_transparency=", string(cmdP.TransparencyLevel)}, ""))
}
params = append(params, ".png")
download = true
}
params = append(prefix, strings.Join(params, ""))
return strings.Join(params, ""), download, nil
}
// TODO:
// * Fix the description for help
//
// Create a function to parse all the command line parameters
// provided and save them in the parameter struct.
func flagParser(cmdP *cmdParams) {
metric := flag.Bool("metric", false, "Display weather in metric")
uscs := flag.Bool("uscs", false, "Display weather Imperial")
meterSecond := flag.Bool("meter-second", false, "Display wind in m/s")
zero := flag.Bool("zero", false, "Show the weather now")
one := flag.Bool("one", false, "Show the weather for one day")
two := flag.Bool("two", false, "Show the weather for two days")
ignoreUserAgent := flag.Bool("ignore-user-agent", false, "Request ignoring the user agent")
followLink := flag.Bool("follow-link", true, "Follow link redirect")
narrow := flag.Bool("narrow", false, "Display weather in narrow view")
quiet := flag.Bool("quiet", false, "Add the quiet flag")
superQuiet := flag.Bool("super-quiet", false, "Add the super quiet flag")
noColors := flag.Bool("no-colors", false, "Disable displaying colors (always enabled on windows)")
addFrame := flag.Bool("add-frame", false, "Add a frame to the output")
midTransparency := flag.Bool("mid-transparency", false, "Enable mid-transparency (PNG only)")
transparency := flag.Bool("transparency", false, "Enable transparency (PNG only)")
png := flag.Bool("png", false, "Download a weather PNG image")
v2 := flag.Bool("v2", false, "Use the v2 endpoint")
transparencyLevel := flag.Int("transparency-level", 0, "Set transparency level between 0 and 100 ")
location := flag.String("location", "", "Specify explicite location")
language := flag.String("language", "", "Speficy explicite language")
flag.Parse()
// Windows does not have color encoding
// so let's make sure windows users are happy
term := os.Getenv("TERM")
if term == "" {
*noColors = true
}
if *metric {
cmdP.Flags += METRIC
}
if *uscs {
cmdP.Flags += USCS
}
if *meterSecond {
cmdP.Flags += METERSECOND
}
if *zero {
cmdP.Flags += ZERO
}
if *one {
cmdP.Flags += ONE
}
if *two {
cmdP.Flags += TWO
}
if *ignoreUserAgent {
cmdP.Flags += IGNOREUSERAGENT
}
if *followLink {
cmdP.Flags += FOLLOWLINK
}
if *narrow {
cmdP.Flags += NARROW
}
if *quiet {
cmdP.Flags += QUIET
}
if *superQuiet {
cmdP.Flags += SUPERQUIET
}
if *noColors {
cmdP.Flags += NOCOLORS
}
if *addFrame {
cmdP.Flags += ADDFRAME
}
if *midTransparency {
cmdP.Flags += MIDTRANSPARENCY
}
if *transparency {
cmdP.Flags += TRANSPARENCY
cmdP.TransparencyLevel = *transparencyLevel
}
if *png {
cmdP.Flags += PNG
}
if *v2 {
cmdP.Flags += VTWO
}
if *location != "" {
cmdP.Flags += LOCATION
cmdP.Location = *location
}
if *language != "" {
cmdP.Flags += LANGUAGE
cmdP.Language = *language
}
}
// Create a function to generate the url that we'll be calling shortly.
func generateURL(domain string, v2 bool, location string, lang string, format string) ([]string, map[string]string) {
var link []string
var headers = make(map[string]string)
link = append(link, "https://")
if v2 {
link = append(link, "v2.")
}
link = append(link, domain, "/")
if location != "" {
link = append(link, location)
}
if format != "" {
link = append(link, format)
}
if lang != "" {
headers["Accept-Language"] = lang
}
return link, headers
}
// We create a weather struct with the url
type weather struct {
url string
@ -370,6 +138,246 @@ func getWheather(url string, headers map[string]string, download bool) {
w.get(req, download)
}
// Define a command parameters struct.
// This will hold all the configurations
// set by the command line parameters provided.
type cmdParams struct {
Flags int
Transparency int
Location string
Language string
}
// Create a method to figure out the command parameters
// provided and translate them into uri parameters.
// This will figure out if we need to download the PNG image
// as well.
func (cmdP *cmdParams) generateParamFormat() (string, bool, error) {
var params []string
var prefix []string
var download bool
prefix = append(prefix, "?")
if cmdP.Flags&METRIC == METRIC {
params = append(params, MetricSwitch)
}
if cmdP.Flags&USCS == USCS {
params = append(params, UscsSwitch)
}
if cmdP.Flags&METERSECOND == METERSECOND {
params = append(params, MeterSecondSwitch)
}
if cmdP.Flags&ZERO == ZERO {
params = append(params, ZeroSwitch)
}
if cmdP.Flags&ONE == ONE {
params = append(params, OneSwitch)
}
if cmdP.Flags&TWO == TWO {
params = append(params, TwoSwitch)
}
if cmdP.Flags&IGNOREUSERAGENT == IGNOREUSERAGENT {
params = append(params, IgnoreUserAgentSwitch)
}
if cmdP.Flags&FOLLOWLINK == FOLLOWLINK {
params = append(params, FollowLinkSwitch)
}
if cmdP.Flags&NARROW == NARROW {
params = append(params, NarrowSwitch)
}
if cmdP.Flags&QUIET == QUIET {
params = append(params, QuietSwitch)
}
if cmdP.Flags&SUPERQUIET == SUPERQUIET {
params = append(params, SuperQuietSwitch)
}
if cmdP.Flags&NOCOLORS == NOCOLORS {
params = append(params, NoColorsSwitch)
}
if cmdP.Flags&ADDFRAME == ADDFRAME {
params = append(params, AddFrameSwitch)
}
if cmdP.Flags&PNG == PNG {
prefix[0] = "_"
if cmdP.Flags&MIDTRANSPARENCY == MIDTRANSPARENCY {
params = append(params, MidTransparencySwitch)
}
if cmdP.Transparency >= 0 && cmdP.Transparency <= 100 {
params = append(params, strings.Join([]string{"_transparency=", string(cmdP.Transparency)}, ""))
}
params = append(params, ".png")
download = true
}
params = append(prefix, strings.Join(params, ""))
return strings.Join(params, ""), download, nil
}
// Defining the command-line interface
var (
app = kingpin.New("go-cmw", "A small terminal wrapper around the wttr.in weather endpoint.")
metric = app.Flag("metric", "Display weather in metric").Short('m').Default("false").Bool()
uscs = app.Flag("uscs", "Display weather in imperial").Short('u').Default("false").Bool()
meterSecond = app.Flag("meter-second", "Display wind in m/s").Short('M').Default("false").Bool()
zero = app.Flag("zero", "Show the weather now").Short('z').Default("false").Bool()
one = app.Flag("one", "Show the weather for one day").Short('o').Default("false").Bool()
two = app.Flag("two", "Show the weather for two days").Short('w').Default("false").Bool()
ignoreUserAgent = app.Flag("ignore-user-agent", "Request ignoring the user agent").Short('A').Default("false").Bool()
followLink = app.Flag("follow-link", "Follow link redirect").Short('F').Default("true").Bool()
narrow = app.Flag("narrow", "Display weather in narrow view").Short('n').Default("false").Bool()
quiet = app.Flag("quiet", "Add the quiet flag").Short('q').Default("false").Bool()
superQuiet = app.Flag("super-quiet", "Add the super quiet flag").Short('Q').Default("false").Bool()
noColors = app.Flag("no-colors", "Disable displaying colors (always enabled on windows").Short('N').Default("false").Bool()
addFrame = app.Flag("add-frame", "Add a frame to the output").Short('p').Default("false").Bool()
midTransparency = app.Flag("mid-transparency", "Enable mid-transparency (PNG only)").Short('T').Default("false").Bool()
png = app.Flag("png", "Download a weather PNG image").Short('P').Default("false").Bool()
v2 = app.Flag("v2", "Use the v2 endpoint").Default("false").Bool()
transparency = app.Flag("transparency", "Set transparency level (0-100) (PNG only)").Short('t').Default("0").Int()
location = app.Flag("location", "Specify explicite location").Short('L').String()
language = app.Flag("language", "Specify explicite language").Short('l').String()
)
// Create a function to parse all the command line parameters
// provided and save them in the parameter struct.
func flagParser(cmdP *cmdParams) {
app.Version(VERSION)
app.Author(AUTHOR)
kingpin.MustParse(app.Parse(os.Args[1:]))
// Windows does not have color encoding
// so let's make sure windows users are happy
term := os.Getenv("TERM")
if term == "" {
*noColors = true
}
if *metric {
cmdP.Flags += METRIC
}
if *uscs {
cmdP.Flags += USCS
}
if *meterSecond {
cmdP.Flags += METERSECOND
}
if *zero {
cmdP.Flags += ZERO
}
if *one {
cmdP.Flags += ONE
}
if *two {
cmdP.Flags += TWO
}
if *ignoreUserAgent {
cmdP.Flags += IGNOREUSERAGENT
}
if *followLink {
cmdP.Flags += FOLLOWLINK
}
if *narrow {
cmdP.Flags += NARROW
}
if *quiet {
cmdP.Flags += QUIET
}
if *superQuiet {
cmdP.Flags += SUPERQUIET
}
if *noColors {
cmdP.Flags += NOCOLORS
}
if *addFrame {
cmdP.Flags += ADDFRAME
}
if *midTransparency {
cmdP.Flags += MIDTRANSPARENCY
}
if *transparency >= 0 && *transparency <= 100 {
cmdP.Transparency = *transparency
}
if *png {
cmdP.Flags += PNG
}
if *v2 {
cmdP.Flags += VTWO
}
if *location != "" {
cmdP.Flags += LOCATION
cmdP.Location = *location
}
if *language != "" {
cmdP.Flags += LANGUAGE
cmdP.Language = *language
}
}
// Create a function to generate the url that we'll be calling shortly.
func generateURL(domain string, v2 bool, location string, lang string, format string) ([]string, map[string]string) {
var link []string
var headers = make(map[string]string)
link = append(link, "https://")
if v2 {
link = append(link, "v2.")
}
link = append(link, domain, "/")
if location != "" {
link = append(link, location)
}
if format != "" {
link = append(link, format)
}
if lang != "" {
headers["Accept-Language"] = lang
}
return link, headers
}
// This is the main function that glues everything together.
func main() {
var params cmdParams = cmdParams{}