Skip to content

Commit f33d5fe

Browse files
committed
Remove unused flags, add --format flag
1 parent 6e75ce6 commit f33d5fe

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist
2+
luas-cli

cmd/forecast.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"github.com/amitizle/go-luas"
5-
"github.com/amitizle/go-luas-cli/internal/output"
6+
"github.com/amitizle/luas-cli/internal/output"
67
"github.com/spf13/cobra"
78
"os"
89
)
@@ -31,15 +32,30 @@ func forecast(cmd *cobra.Command, args []string) {
3132
output.Errorf("error getting forecast for stop %s, %v", stop.Name, err)
3233
os.Exit(1)
3334
}
34-
output.Infof("Forecast for stop %s from %s\n\n%s\n",
35-
stop.Name,
36-
stopInfo.Created,
37-
stopInfo.Message)
38-
for _, direction := range stopInfo.Directions {
39-
output.Infof("Direction: %s", direction.Name)
40-
for _, tram := range direction.Trams {
41-
output.Infof("Destination: %s, due in %s minutes", tram.Destination, tram.DueMins)
35+
printOutput(cmd.Flag("format").Value.String(), stop, stopInfo)
36+
}
37+
38+
func printOutput(format string, stop *luas.Stop, stopInfo *luas.StopInfo) {
39+
switch format {
40+
case "table":
41+
output.Infof("Forecast for stop %s from %s\n\n%s\n",
42+
stop.Name,
43+
stopInfo.Created,
44+
stopInfo.Message)
45+
for _, direction := range stopInfo.Directions {
46+
output.Infof("Direction: %s", direction.Name)
47+
for _, tram := range direction.Trams {
48+
output.Infof("Destination: %s, due in %s minutes", tram.Destination, tram.DueMins)
49+
}
50+
output.Infof("")
51+
}
52+
case "json":
53+
jsonOutput, err := json.Marshal(stopInfo)
54+
if err != nil {
55+
output.Errorf("while generating json output: %v", err)
56+
os.Exit(1)
4257
}
43-
output.Infof("")
58+
output.NoFormat(string(jsonOutput))
59+
default:
4460
}
4561
}

cmd/luas.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,8 @@ func Execute() {
2727

2828
func init() {
2929
cobra.OnInitialize(initConfig)
30-
31-
// Here you will define your flags and configuration settings.
32-
// Cobra supports persistent flags, which, if defined here,
33-
// will be global for your application.
34-
luasCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-luas-cli.yaml)")
35-
36-
// Cobra also supports local flags, which will only run
37-
// when this action is called directly.
38-
luasCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
30+
luasCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.luas.yaml)")
31+
luasCmd.PersistentFlags().StringP("format", "f", "table", "output format (table/json)")
3932
}
4033

4134
// initConfig reads in config file and ENV variables if set.
@@ -51,9 +44,9 @@ func initConfig() {
5144
os.Exit(1)
5245
}
5346

54-
// Search config in home directory with name ".go-luas-cli" (without extension).
47+
// Search config in home directory with name ".luas" (without extension).
5548
viper.AddConfigPath(home)
56-
viper.SetConfigName(".go-luas-cli")
49+
viper.SetConfigName(".luas")
5750
}
5851

5952
viper.AutomaticEnv() // read in environment variables that match

internal/output/output.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ func Warn(line string) {
2828
func Warnf(line string, args ...interface{}) {
2929
Warn(fmt.Sprintf(line, args...))
3030
}
31+
32+
func NoFormat(line string) {
33+
fmt.Println(line)
34+
}
35+
36+
func NoFormatf(line string, args ...interface{}) {
37+
NoFormat(fmt.Sprintf(line, args...))
38+
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package main
22

3-
import "github.com/amitizle/go-luas-cli/cmd"
3+
import "github.com/amitizle/luas-cli/cmd"
44

55
func main() {
66
cmd.Execute()

0 commit comments

Comments
 (0)