Skip to content

Commit

Permalink
chore: move to generic tabwritter (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod authored Jan 30, 2024
1 parent 987916b commit 59b5466
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- '**.go'
jobs:
GolangCI-Lint:
runs-on: ubuntu-latest
runs-on: macos-14
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
Expand Down
10 changes: 0 additions & 10 deletions cmd/glctl/cmd/delete-link.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,4 @@ var deleteLinkCmd = &cobra.Command{

func init() {
deleteCmd.AddCommand(deleteLinkCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// linkCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// linkCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
39 changes: 13 additions & 26 deletions cmd/glctl/cmd/get-label.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package glctl

import (
"context"
"fmt"
"log"
"os"
"text/tabwriter"

"github.com/orange-cloudavenue/common-go/print"
"github.com/spf13/cobra"

"github.com/azrod/golink/models"
Expand All @@ -22,14 +19,9 @@ var getLabelCmd = &cobra.Command{
Short: "Get labels",
Long: `Return a list of labels or a single label by name or ID.`,
Run: func(cmd *cobra.Command, args []string) {
initSDK()

sdk := initSDK()
sdk.SetNamespace(globalFlagNamespace)

// Create a new context with timeout
ctx, cancel := context.WithTimeout(context.Background(), globalTimeout())
defer cancel()

var labels []struct {
Label models.Label
Links []models.Link
Expand All @@ -38,7 +30,7 @@ var getLabelCmd = &cobra.Command{
if len(args) > 0 {
if models.IsValidUUID(args[0]) {
// Arg is not an ID, try to find the label by name
label, err := sdk.GetLabelByID(ctx, models.LabelID(args[0]))
label, err := sdk.GetLabelByID(cmd.Context(), models.LabelID(args[0]))
if err != nil {
log.Default().Printf("Failed to get label: %s", err)
return
Expand All @@ -49,7 +41,7 @@ var getLabelCmd = &cobra.Command{
Links []models.Link
}{Label: label})
} else {
label, err := sdk.GetLabelByName(ctx, args[0])
label, err := sdk.GetLabelByName(cmd.Context(), args[0])
if err != nil {
log.Default().Printf("Failed to get label: %s", err)
return
Expand All @@ -61,7 +53,7 @@ var getLabelCmd = &cobra.Command{
}{Label: label})
}
} else {
listLabels, err := sdk.GetLabels(ctx)
listLabels, err := sdk.GetLabels(cmd.Context())
if err != nil {
log.Default().Printf("Failed to get labels: %s", err)
return
Expand All @@ -75,36 +67,31 @@ var getLabelCmd = &cobra.Command{
}

for i, label := range labels {
links, err := sdk.GetLinksAssociatedToLabel(ctx, label.Label.ID)
links, err := sdk.GetLinksAssociatedToLabel(cmd.Context(), label.Label.ID)
if err != nil {
log.Default().Printf("Failed to get links associated with label: %s", err)
return
}
labels[i].Links = links
}

p := print.New()
defer p.PrintTable()

switch globalFlagOutput {
case globalFlagOutputShort:
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
fs := "%s\t%s\n"
fmt.Fprintf(w, fs, "NAME", "LINKS")
p.SetHeader("NAME", "LINKS")

for _, l := range labels {
fmt.Fprintf(w, fs, l.Label.Name, fmt.Sprintf("%d", len(l.Links)))
p.AddFields(l.Label.Name, len(l.Links))
}

w.Flush()

case globalFlagOutputWide:
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
fs := "%s\t%s\t%s\t%s\n"
fmt.Fprintf(w, fs, "ID", "NAME", "LINKS", "COLOR")
p.SetHeader("ID", "NAME", "LINKS", "COLOR")

for _, l := range labels {
fmt.Fprintf(w, fs, l.Label.ID, l.Label.Name, fmt.Sprintf("%d", len(l.Links)), l.Label.Color)
p.AddFields(l.Label.ID, l.Label.Name, len(l.Links), l.Label.Color)
}

w.Flush()
}
},
}
Expand Down
21 changes: 8 additions & 13 deletions cmd/glctl/cmd/get-link.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package glctl

import (
"fmt"
"log"
"os"
"text/tabwriter"

"github.com/orange-cloudavenue/common-go/print"
"github.com/spf13/cobra"

"github.com/azrod/golink/models"
Expand Down Expand Up @@ -69,29 +67,26 @@ var getLinkCmd = &cobra.Command{
}
}

p := print.New()
defer p.PrintTable()

switch globalFlagOutput {
case globalFlagOutputShort:
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
fs := "%s\t%s\t%s\t%s\t%s\n"
fmt.Fprintf(w, fs, "NAMESPACE", "NAME", "PATH", "TARGET URL", "STATUS")
p.SetHeader("NAMESPACE", "NAME", "PATH", "TARGET URL", "STATUS")

for _, l := range links {
if len(l.TargetURL) > 50 {
l.TargetURL = l.TargetURL[:50] + "..."
}
fmt.Fprintf(w, fs, globalFlagNamespace, l.Name, l.SourcePath, l.TargetURL, l.Enabled.String())
p.AddFields(globalFlagNamespace, l.Name, l.SourcePath, l.TargetURL, l.Enabled)
}
w.Flush()

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

for _, l := range links {
fmt.Fprintf(w, fs, globalFlagNamespace, l.Name, l.SourcePath, l.TargetURL, l.Enabled.String(), l.Labels)
p.AddFields(globalFlagNamespace, l.Name, l.SourcePath, l.TargetURL, l.Enabled, l.Labels)
}
w.Flush()
}
},
}
Expand Down
14 changes: 5 additions & 9 deletions cmd/glctl/cmd/get-namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package glctl

import (
"fmt"
"log"
"os"
"text/tabwriter"

"github.com/orange-cloudavenue/common-go/print"
"github.com/spf13/cobra"

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

switch globalFlagOutput {
case globalFlagOutputShort, globalFlagOutputWide:
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
fs := "%s\t%s\t%s\n"
fmt.Fprintf(w, fs, "NAME", "STATUS", "LINKS")

p := print.New()
p.SetHeader("NAME", "STATUS", "LINKS")
for _, l := range nss {
fmt.Fprintf(w, fs, l.Name, l.Enabled, fmt.Sprintf("%d", len(l.Links)))
p.AddFields(l.Name, l.Enabled, len(l.Links))
}
w.Flush()
p.PrintTable()
}
},
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/glctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ var commit = "none"
// date that can be overwritten by a release process.
var date = "unknown"

var sdk *golink.Client

var apiURL = "http://localhost:8081"

// rootCmd represents the base command when called without any subcommands.
Expand Down Expand Up @@ -58,6 +56,8 @@ var (
const (
globalFlagOutputShort = "short"
globalFlagOutputWide = "wide"

dirName = ".golink"
)

var (
Expand Down Expand Up @@ -140,13 +140,13 @@ func initConfig() {
}

// Search config in $HOME/.golink
viper.AddConfigPath(home + "/.golink")
viper.AddConfigPath(home + "/" + dirName)
viper.SetConfigName("config")

// Check if directory exists.
if _, err := os.Stat(home + "/.golink"); os.IsNotExist(err) {
if err := os.MkdirAll(home+"/.golink", 0o755); err != nil {
log.Printf("Can't create config directory(dir:%s): %s", home+"/.golink", err)
if _, err := os.Stat(home + "/" + dirName); os.IsNotExist(err) {
if err := os.MkdirAll(home+"/"+dirName, 0o755); err != nil {
log.Printf("Can't create config directory(dir:%s): %s", home+"/"+dirName, err)
os.Exit(1)
}
}
Expand Down
3 changes: 0 additions & 3 deletions cmd/glctl/cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package glctl

import (
Expand Down
20 changes: 6 additions & 14 deletions cmd/glctl/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package glctl

import (
"log"

"github.com/orange-cloudavenue/common-go/print"
"github.com/spf13/cobra"
)

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

log.Printf(`Client informations:
Version: %s
Commit: %s
Build Date: %s`,
version, commit, date)
p := print.New()
defer p.PrintTable()
p.SetHeader("Type", "Version", "Commit", "Build Date")
p.AddFields("Client", version, commit, date)

if vServer != "" {
log.Printf(`
Server informations:
Version: %s
`, vServer)
p.AddFields("Server", vServer, "", "")
}
},
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/orange-cloudavenue/common-go/print v0.0.0-20240129085602-f04f55fc74ff // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/num30/config v0.1.2 h1:FCH7WapA/YejX5EU8peeMNWrZDqfIF8JgaatR+Mxf1w=
github.com/num30/config v0.1.2/go.mod h1:CIFhchwXwqNsgLneQ/ZVtPZUIQeKACWzqiYNdoisRks=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/orange-cloudavenue/common-go/print v0.0.0-20240129085602-f04f55fc74ff h1:8XNeH2ssPlKVauaHToG5EsfXLrtJXshvTg19NLPKzvs=
github.com/orange-cloudavenue/common-go/print v0.0.0-20240129085602-f04f55fc74ff/go.mod h1:IYtCusqpEGS0dhC6F8X9GHrrt1gp1zHaNhSKGYV59Xg=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
Expand Down
30 changes: 0 additions & 30 deletions pkg/sb/utils.go

This file was deleted.

0 comments on commit 59b5466

Please sign in to comment.