Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Environment subcommands #89

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
Add tab:"omit" struct tag
  • Loading branch information
cmoog committed Aug 1, 2020
commit a27700e2876965d595bd2943d03aa6b1f754b6b8
1 change: 0 additions & 1 deletion cmd/coder/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func makeSecretsCmd() cli.Command {
Action: viewSecret,
},
},
Flags: nil,
}
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/coder/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import (
func makeSyncCmd() cli.Command {
var init bool
return cli.Command{
Name: "sync",
Usage: "Synchronize local files to a Coder environment",
Description: "Establish a one way directory sync to a Coder environment.",
ArgsUsage: "[local directory] [<env name>:<remote directory>]",
Name: "sync",
Usage: "Establish a one way directory sync to a Coder environment",
ArgsUsage: "[local directory] [<env name>:<remote directory>]",
Before: func(c *cli.Context) error {
if c.Args().Get(0) == "" || c.Args().Get(1) == "" {
return xerrors.Errorf("[local] and [remote] arguments are required")
Expand Down
1 change: 0 additions & 1 deletion cmd/coder/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func makeUsersCmd() cli.Command {
},
},
},
HelpName: "",
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/entclient/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

// Secret describes a Coder secret
type Secret struct {
ID string `json:"id"`
ID string `json:"id" tab:"omit"`
Name string `json:"name"`
Value string `json:"value,omitempty"`
Description string `json:"description"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
UpdatedAt time.Time `json:"updated_at" tab:"omit"`
}

// Secrets gets all secrets owned by the authed user
Expand Down
25 changes: 21 additions & 4 deletions internal/x/xtabwriter/tabwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,44 @@ import (
"text/tabwriter"
)

// NewWriter chooses reasonable defaults for a human readable output of tabular data
const structFieldTagKey = "tab"

// NewWriter chooses reasonable defaults for a human readable output of tabular data.
func NewWriter() *tabwriter.Writer {
return tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
}

// StructValues tab delimits the values of a given struct
// StructValues tab delimits the values of a given struct.
//
// Tag a field `tab:"omit"` to hide it from output.
func StructValues(data interface{}) string {
v := reflect.ValueOf(data)
s := &strings.Builder{}
for i := 0; i < v.NumField(); i++ {
if shouldHideField(v.Type().Field(i)) {
continue
}
s.WriteString(fmt.Sprintf("%s\t", v.Field(i).Interface()))
}
return s.String()
}

// StructFieldNames tab delimits the field names of a given struct
// StructFieldNames tab delimits the field names of a given struct.
//
// Tag a field `tab:"omit"` to hide it from output.
func StructFieldNames(data interface{}) string {
v := reflect.ValueOf(data)
s := &strings.Builder{}
for i := 0; i < v.NumField(); i++ {
s.WriteString(fmt.Sprintf("%s\t", v.Type().Field(i).Name))
field := v.Type().Field(i)
if shouldHideField(field) {
continue
}
s.WriteString(fmt.Sprintf("%s\t", field.Name))
}
return s.String()
}

func shouldHideField(f reflect.StructField) bool {
return f.Tag.Get(structFieldTagKey) == "omit"
}