Skip to content

Commit d90369d

Browse files
committed
chore: move to generic tabwritter
1 parent 987916b commit d90369d

File tree

11 files changed

+42
-112
lines changed

11 files changed

+42
-112
lines changed

.github/workflows/checks-go.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- '**.go'
77
jobs:
88
GolangCI-Lint:
9-
runs-on: ubuntu-latest
9+
runs-on: macos-14
1010
steps:
1111
- uses: actions/checkout@v4
1212
- uses: actions/setup-go@v5

cmd/glctl/cmd/delete-link.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,4 @@ var deleteLinkCmd = &cobra.Command{
3939

4040
func init() {
4141
deleteCmd.AddCommand(deleteLinkCmd)
42-
43-
// Here you will define your flags and configuration settings.
44-
45-
// Cobra supports Persistent Flags which will work for this command
46-
// and all subcommands, e.g.:
47-
// linkCmd.PersistentFlags().String("foo", "", "A help for foo")
48-
49-
// Cobra supports local flags which will only run when this command
50-
// is called directly, e.g.:
51-
// linkCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
5242
}

cmd/glctl/cmd/get-label.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
44
package glctl
55

66
import (
7-
"context"
8-
"fmt"
97
"log"
10-
"os"
11-
"text/tabwriter"
128

9+
"github.com/orange-cloudavenue/common-go/print"
1310
"github.com/spf13/cobra"
1411

1512
"github.com/azrod/golink/models"
@@ -22,14 +19,9 @@ var getLabelCmd = &cobra.Command{
2219
Short: "Get labels",
2320
Long: `Return a list of labels or a single label by name or ID.`,
2421
Run: func(cmd *cobra.Command, args []string) {
25-
initSDK()
26-
22+
sdk := initSDK()
2723
sdk.SetNamespace(globalFlagNamespace)
2824

29-
// Create a new context with timeout
30-
ctx, cancel := context.WithTimeout(context.Background(), globalTimeout())
31-
defer cancel()
32-
3325
var labels []struct {
3426
Label models.Label
3527
Links []models.Link
@@ -38,7 +30,7 @@ var getLabelCmd = &cobra.Command{
3830
if len(args) > 0 {
3931
if models.IsValidUUID(args[0]) {
4032
// Arg is not an ID, try to find the label by name
41-
label, err := sdk.GetLabelByID(ctx, models.LabelID(args[0]))
33+
label, err := sdk.GetLabelByID(cmd.Context(), models.LabelID(args[0]))
4234
if err != nil {
4335
log.Default().Printf("Failed to get label: %s", err)
4436
return
@@ -49,7 +41,7 @@ var getLabelCmd = &cobra.Command{
4941
Links []models.Link
5042
}{Label: label})
5143
} else {
52-
label, err := sdk.GetLabelByName(ctx, args[0])
44+
label, err := sdk.GetLabelByName(cmd.Context(), args[0])
5345
if err != nil {
5446
log.Default().Printf("Failed to get label: %s", err)
5547
return
@@ -61,7 +53,7 @@ var getLabelCmd = &cobra.Command{
6153
}{Label: label})
6254
}
6355
} else {
64-
listLabels, err := sdk.GetLabels(ctx)
56+
listLabels, err := sdk.GetLabels(cmd.Context())
6557
if err != nil {
6658
log.Default().Printf("Failed to get labels: %s", err)
6759
return
@@ -75,36 +67,31 @@ var getLabelCmd = &cobra.Command{
7567
}
7668

7769
for i, label := range labels {
78-
links, err := sdk.GetLinksAssociatedToLabel(ctx, label.Label.ID)
70+
links, err := sdk.GetLinksAssociatedToLabel(cmd.Context(), label.Label.ID)
7971
if err != nil {
8072
log.Default().Printf("Failed to get links associated with label: %s", err)
8173
return
8274
}
8375
labels[i].Links = links
8476
}
8577

78+
p := print.New()
79+
defer p.PrintTable()
80+
8681
switch globalFlagOutput {
8782
case globalFlagOutputShort:
88-
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
89-
fs := "%s\t%s\n"
90-
fmt.Fprintf(w, fs, "NAME", "LINKS")
83+
p.SetHeader("NAME", "LINKS")
9184

9285
for _, l := range labels {
93-
fmt.Fprintf(w, fs, l.Label.Name, fmt.Sprintf("%d", len(l.Links)))
86+
p.AddFields(l.Label.Name, len(l.Links))
9487
}
9588

96-
w.Flush()
97-
9889
case globalFlagOutputWide:
99-
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
100-
fs := "%s\t%s\t%s\t%s\n"
101-
fmt.Fprintf(w, fs, "ID", "NAME", "LINKS", "COLOR")
90+
p.SetHeader("ID", "NAME", "LINKS", "COLOR")
10291

10392
for _, l := range labels {
104-
fmt.Fprintf(w, fs, l.Label.ID, l.Label.Name, fmt.Sprintf("%d", len(l.Links)), l.Label.Color)
93+
p.AddFields(l.Label.ID, l.Label.Name, len(l.Links), l.Label.Color)
10594
}
106-
107-
w.Flush()
10895
}
10996
},
11097
}

cmd/glctl/cmd/get-link.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
44
package glctl
55

66
import (
7-
"fmt"
87
"log"
9-
"os"
10-
"text/tabwriter"
118

9+
"github.com/orange-cloudavenue/common-go/print"
1210
"github.com/spf13/cobra"
1311

1412
"github.com/azrod/golink/models"
@@ -69,29 +67,26 @@ var getLinkCmd = &cobra.Command{
6967
}
7068
}
7169

70+
p := print.New()
71+
defer p.PrintTable()
72+
7273
switch globalFlagOutput {
7374
case globalFlagOutputShort:
74-
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
75-
fs := "%s\t%s\t%s\t%s\t%s\n"
76-
fmt.Fprintf(w, fs, "NAMESPACE", "NAME", "PATH", "TARGET URL", "STATUS")
75+
p.SetHeader("NAMESPACE", "NAME", "PATH", "TARGET URL", "STATUS")
7776

7877
for _, l := range links {
7978
if len(l.TargetURL) > 50 {
8079
l.TargetURL = l.TargetURL[:50] + "..."
8180
}
82-
fmt.Fprintf(w, fs, globalFlagNamespace, l.Name, l.SourcePath, l.TargetURL, l.Enabled.String())
81+
p.AddFields(globalFlagNamespace, l.Name, l.SourcePath, l.TargetURL, l.Enabled)
8382
}
84-
w.Flush()
8583

8684
case globalFlagOutputWide:
87-
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
88-
fs := "%s\t%s\t%s\t%s\t%s\t%s\n"
89-
fmt.Fprintf(w, fs, "NAMESPACE", "NAME", "PATH", "TARGET URL", "STATUS", "LABELS")
85+
p.SetHeader("NAMESPACE", "NAME", "PATH", "TARGET URL", "STATUS", "LABELS")
9086

9187
for _, l := range links {
92-
fmt.Fprintf(w, fs, globalFlagNamespace, l.Name, l.SourcePath, l.TargetURL, l.Enabled.String(), l.Labels)
88+
p.AddFields(globalFlagNamespace, l.Name, l.SourcePath, l.TargetURL, l.Enabled, l.Labels)
9389
}
94-
w.Flush()
9590
}
9691
},
9792
}

cmd/glctl/cmd/get-namespace.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
44
package glctl
55

66
import (
7-
"fmt"
87
"log"
9-
"os"
10-
"text/tabwriter"
118

9+
"github.com/orange-cloudavenue/common-go/print"
1210
"github.com/spf13/cobra"
1311

1412
"github.com/azrod/golink/models"
@@ -71,14 +69,12 @@ $> glctl get namespace [NAME] [NAME] [NAME]`,
7169

7270
switch globalFlagOutput {
7371
case globalFlagOutputShort, globalFlagOutputWide:
74-
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
75-
fs := "%s\t%s\t%s\n"
76-
fmt.Fprintf(w, fs, "NAME", "STATUS", "LINKS")
77-
72+
p := print.New()
73+
p.SetHeader("NAME", "STATUS", "LINKS")
7874
for _, l := range nss {
79-
fmt.Fprintf(w, fs, l.Name, l.Enabled, fmt.Sprintf("%d", len(l.Links)))
75+
p.AddFields(l.Name, l.Enabled, len(l.Links))
8076
}
81-
w.Flush()
77+
p.PrintTable()
8278
}
8379
},
8480
}

cmd/glctl/cmd/root.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ var commit = "none"
2222
// date that can be overwritten by a release process.
2323
var date = "unknown"
2424

25-
var sdk *golink.Client
26-
2725
var apiURL = "http://localhost:8081"
2826

2927
// rootCmd represents the base command when called without any subcommands.
@@ -58,6 +56,8 @@ var (
5856
const (
5957
globalFlagOutputShort = "short"
6058
globalFlagOutputWide = "wide"
59+
60+
dirName = ".golink"
6161
)
6262

6363
var (
@@ -140,13 +140,13 @@ func initConfig() {
140140
}
141141

142142
// Search config in $HOME/.golink
143-
viper.AddConfigPath(home + "/.golink")
143+
viper.AddConfigPath(home + "/" + dirName)
144144
viper.SetConfigName("config")
145145

146146
// Check if directory exists.
147-
if _, err := os.Stat(home + "/.golink"); os.IsNotExist(err) {
148-
if err := os.MkdirAll(home+"/.golink", 0o755); err != nil {
149-
log.Printf("Can't create config directory(dir:%s): %s", home+"/.golink", err)
147+
if _, err := os.Stat(home + "/" + dirName); os.IsNotExist(err) {
148+
if err := os.MkdirAll(home+"/"+dirName, 0o755); err != nil {
149+
log.Printf("Can't create config directory(dir:%s): %s", home+"/"+dirName, err)
150150
os.Exit(1)
151151
}
152152
}

cmd/glctl/cmd/update.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/*
2-
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3-
*/
41
package glctl
52

63
import (

cmd/glctl/cmd/version.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
/*
2-
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3-
*/
41
package glctl
52

63
import (
7-
"log"
8-
4+
"github.com/orange-cloudavenue/common-go/print"
95
"github.com/spf13/cobra"
106
)
117

@@ -21,17 +17,13 @@ var versionCmd = &cobra.Command{
2117
// Ignore errors, we don't care if the server is not reachable
2218
vServer, _ := sdk.GetVersion(cmd.Context())
2319

24-
log.Printf(`Client informations:
25-
Version: %s
26-
Commit: %s
27-
Build Date: %s`,
28-
version, commit, date)
20+
p := print.New()
21+
defer p.PrintTable()
22+
p.SetHeader("Type", "Version", "Commit", "Build Date")
23+
p.AddFields("Client", version, commit, date)
2924

3025
if vServer != "" {
31-
log.Printf(`
32-
Server informations:
33-
Version: %s
34-
`, vServer)
26+
p.AddFields("Server", vServer, "", "")
3527
}
3628
},
3729
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ require (
4444
github.com/mattn/go-colorable v0.1.13 // indirect
4545
github.com/mattn/go-isatty v0.0.20 // indirect
4646
github.com/mitchellh/mapstructure v1.5.0 // indirect
47+
github.com/orange-cloudavenue/common-go/print v0.0.0-20240129085602-f04f55fc74ff // indirect
4748
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
4849
github.com/pkg/errors v0.9.1 // indirect
4950
github.com/sagikazarmark/locafero v0.4.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
211211
github.com/num30/config v0.1.2 h1:FCH7WapA/YejX5EU8peeMNWrZDqfIF8JgaatR+Mxf1w=
212212
github.com/num30/config v0.1.2/go.mod h1:CIFhchwXwqNsgLneQ/ZVtPZUIQeKACWzqiYNdoisRks=
213213
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
214+
github.com/orange-cloudavenue/common-go/print v0.0.0-20240129085602-f04f55fc74ff h1:8XNeH2ssPlKVauaHToG5EsfXLrtJXshvTg19NLPKzvs=
215+
github.com/orange-cloudavenue/common-go/print v0.0.0-20240129085602-f04f55fc74ff/go.mod h1:IYtCusqpEGS0dhC6F8X9GHrrt1gp1zHaNhSKGYV59Xg=
214216
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
215217
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
216218
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=

pkg/sb/utils.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)