Skip to content

Commit

Permalink
main: add support for json output with -o and --output (json | table)…
Browse files Browse the repository at this point in the history
… omitting is default for table

add support for ldflags to supply version and build info when releasing binaries
  • Loading branch information
frederikhs committed Nov 2, 2022
1 parent 4cd8816 commit e7968b4
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 48 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@ jobs:
goos: windows
steps:
- uses: actions/checkout@v3

- name: Set APP_VERSION env
run: echo APP_VERSION=$(echo ${GITHUB_REF} | rev | cut -d'/' -f 1 | rev ) >> ${GITHUB_ENV}

- name: Set BUILD_TIME env
run: echo BUILD_TIME=$(date) >> ${GITHUB_ENV}

- name: Environment Printer
uses: managedkaos/[email protected]

- uses: wangyoucao577/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
md5sum: FALSE
ldflags: -X "main.appVersion=${{ env.APP_VERSION }}" -X "main.buildTime=${{ env.BUILD_TIME }}" -X main.gitCommit=${{ github.sha }} -X main.gitRef=${{ github.ref }}
55 changes: 46 additions & 9 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"os"
"strings"
"time"
)

var addCmd = &cobra.Command{
Expand All @@ -25,18 +28,52 @@ var addCmd = &cobra.Command{
os.Exit(1)
}

cmd.Println(fmt.Sprintf("Successfully added user %s to %s", username, conn.Config.Host))
cmd.Println("\ncredentials")
cmd.Println("Valid until:", validUntil.Format("2006-01-02"))
cmd.Println("Roles:", roles)
cmd.Println("Username:", username)
cmd.Println("Password:", password)
cmd.Println("Hostname:", conn.Config.Host)
cmd.Println("Port:", conn.Config.Port)
cmd.Println("Database:", conn.Config.Database)
output := getOutputType(cmd)
if output == OutputTypeJson {
outputAddJson(cmd, validUntil, roles, username, password, conn.Config.Host, conn.Config.Port, conn.Config.Database)
} else if output == OutputTypeTable {
outputAddTable(cmd, validUntil, roles, username, password, conn.Config.Host, conn.Config.Port, conn.Config.Database)
}
},
}

func outputAddTable(cmd *cobra.Command, validUntil time.Time, roles []string, username, password, host, port, database string) {
cmd.Println(fmt.Sprintf("Successfully added user %s to %s", username, host))
cmd.Println("\ncredentials")
cmd.Println("Valid until:", validUntil.Format("2006-01-02"))
cmd.Println("Roles:", roles)
cmd.Println("Username:", username)
cmd.Println("Password:", password)
cmd.Println("Hostname:", host)
cmd.Println("Port:", port)
cmd.Println("Database:", database)
}

func outputAddJson(cmd *cobra.Command, validUntil time.Time, roles []string, username, password, host, port, database string) {
b, err := json.MarshalIndent(struct {
ValidUntil string `json:"valid_until"`
Roles []string `json:"roles"`
Username string `json:"username"`
Password string `json:"password"`
Hostname string `json:"hostname"`
Port string `json:"port"`
Database string `json:"database"`
}{
ValidUntil: validUntil.Format("2006-01-02"),
Roles: roles,
Username: username,
Password: password,
Hostname: host,
Port: port,
Database: database,
}, "", strings.Repeat(" ", 4))
if err != nil {
panic(err)
}

cmd.Println(string(b))
}

func init() {
rootCmd.AddCommand(addCmd)
addRequiredHostFlag(addCmd)
Expand Down
22 changes: 21 additions & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"os"
"strings"
)

var deleteCmd = &cobra.Command{
Expand All @@ -19,10 +21,28 @@ var deleteCmd = &cobra.Command{
os.Exit(1)
}

cmd.Println(fmt.Sprintf("successfully deleted user %s from %s", username, conn.Config.Host))
output := getOutputType(cmd)
if output == OutputTypeJson {
outputDeleteJson(cmd)
} else if output == OutputTypeTable {
cmd.Println(fmt.Sprintf("successfully deleted user %s from %s", username, conn.Config.Host))
}
},
}

func outputDeleteJson(cmd *cobra.Command) {
b, err := json.MarshalIndent(struct {
Ok bool `json:"ok"`
}{
Ok: true,
}, "", strings.Repeat(" ", 4))
if err != nil {
panic(err)
}

cmd.Println(string(b))
}

func init() {
rootCmd.AddCommand(deleteCmd)
addRequiredHostFlag(deleteCmd)
Expand Down
35 changes: 28 additions & 7 deletions cmd/extend.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"os"
"strings"
"time"
)

var extendCmd = &cobra.Command{
Expand All @@ -19,16 +22,34 @@ var extendCmd = &cobra.Command{
os.Exit(1)
}

cmd.Println(fmt.Sprintf("Successfully extended user %s in %s", username, conn.Config.Host))
cmd.Println("\ncredentials")
cmd.Println("Valid until:", validUntil.Format("2006-01-02"))
cmd.Println("Username:", username)
cmd.Println("Hostname:", conn.Config.Host)
cmd.Println("Port:", conn.Config.Port)
cmd.Println("Database:", conn.Config.Database)
output := getOutputType(cmd)
if output == OutputTypeJson {
outputExtendJson(cmd, validUntil)
} else if output == OutputTypeTable {
cmd.Println(fmt.Sprintf("Successfully extended user %s in %s", username, conn.Config.Host))
cmd.Println("\ncredentials")
cmd.Println("Valid until:", validUntil.Format("2006-01-02"))
cmd.Println("Username:", username)
cmd.Println("Hostname:", conn.Config.Host)
cmd.Println("Port:", conn.Config.Port)
cmd.Println("Database:", conn.Config.Database)
}
},
}

func outputExtendJson(cmd *cobra.Command, validUntil time.Time) {
b, err := json.MarshalIndent(struct {
ValidUntil string `json:"valid_until"`
}{
ValidUntil: validUntil.Format("2006-01-02"),
}, "", strings.Repeat(" ", 4))
if err != nil {
panic(err)
}

cmd.Println(string(b))
}

func init() {
rootCmd.AddCommand(extendCmd)
addRequiredHostFlag(extendCmd)
Expand Down
60 changes: 51 additions & 9 deletions cmd/hosts.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/hiperdk/pg_user/database"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/tg/pgpass"
"os"
"strings"
)

var hostsCmd = &cobra.Command{
Expand All @@ -19,19 +22,58 @@ var hostsCmd = &cobra.Command{
os.Exit(1)
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"host", "port", "database", "username"})

for _, e := range entries {
table.Append([]string{
e.Hostname, e.Port, e.Database, e.Username,
})
output := getOutputType(cmd)
if output == OutputTypeJson {
outputHostsJson(entries, cmd)
} else if output == OutputTypeTable {
outputHostsTable(entries, cmd)
}

table.Render()
},
}

func outputHostsTable(es []pgpass.Entry, cmd *cobra.Command) {
out := cmd.OutOrStdout()

table := tablewriter.NewWriter(out)
table.SetHeader([]string{"host", "port", "database", "username", "password"})

for _, e := range es {
table.Append([]string{
e.Hostname, e.Port, e.Database, e.Username, "********",
})
}

table.Render()
}

type Entry struct {
Hostname string `json:"hostname"`
Port string `json:"port"`
Database string `json:"database"`
Username string `json:"username"`
Password string `json:"password"`
}

func outputHostsJson(es []pgpass.Entry, cmd *cobra.Command) {
var entries []Entry
for _, e := range es {
entries = append(entries, Entry{
Hostname: e.Hostname,
Port: e.Port,
Database: e.Database,
Username: e.Username,
Password: "********",
})
}

b, err := json.MarshalIndent(entries, "", strings.Repeat(" ", 4))
if err != nil {
panic(err)
}

cmd.Println(string(b))
}

func init() {
rootCmd.AddCommand(hostsCmd)
}
26 changes: 22 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"github.com/hiperdk/pg_user/database"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand All @@ -26,17 +27,34 @@ var listCmd = &cobra.Command{
os.Exit(1)
}

listUsersForConnection(conn)
users, err := conn.GetAllUsers()
if err != nil {
cmd.Println(err)
os.Exit(1)
}

output := getOutputType(cmd)
if output == OutputTypeJson {
outputListJson(users, cmd)
} else if output == OutputTypeTable {
outputListTable(users, cmd)
}
},
}

func listUsersForConnection(conn *database.DBConn) {
users, err := conn.GetAllUsers()
func outputListJson(users []database.User, cmd *cobra.Command) {
b, err := json.MarshalIndent(users, "", strings.Repeat(" ", 4))
if err != nil {
panic(err)
}

table := tablewriter.NewWriter(os.Stdout)
cmd.Println(string(b))
}

func outputListTable(users []database.User, cmd *cobra.Command) {
out := cmd.OutOrStdout()

table := tablewriter.NewWriter(out)
table.SetHeader([]string{"username", "valid until", "roles"})

for _, u := range users {
Expand Down
36 changes: 28 additions & 8 deletions cmd/reset.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"os"
"strings"
)

var resetCmd = &cobra.Command{
Expand All @@ -25,17 +27,35 @@ var resetCmd = &cobra.Command{
os.Exit(1)
}

cmd.Println(fmt.Sprintf("Successfully reset user password for %s in %s", username, conn.Config.Host))
cmd.Println("\ncredentials")
cmd.Println("Valid until:", *user.ValidUntil)
cmd.Println("Username:", username)
cmd.Println("Password:", password)
cmd.Println("Hostname:", conn.Config.Host)
cmd.Println("Port:", conn.Config.Port)
cmd.Println("Database:", conn.Config.Database)
output := getOutputType(cmd)
if output == OutputTypeJson {
outputResetJson(cmd, password)
} else if output == OutputTypeTable {
cmd.Println(fmt.Sprintf("Successfully reset user password for %s in %s", username, conn.Config.Host))
cmd.Println("\ncredentials")
cmd.Println("Valid until:", *user.ValidUntil)
cmd.Println("Username:", username)
cmd.Println("Password:", password)
cmd.Println("Hostname:", conn.Config.Host)
cmd.Println("Port:", conn.Config.Port)
cmd.Println("Database:", conn.Config.Database)
}
},
}

func outputResetJson(cmd *cobra.Command, password string) {
b, err := json.MarshalIndent(struct {
Password string `json:"password"`
}{
Password: password,
}, "", strings.Repeat(" ", 4))
if err != nil {
panic(err)
}

cmd.Println(string(b))
}

func init() {
rootCmd.AddCommand(resetCmd)
addRequiredHostFlag(resetCmd)
Expand Down
Loading

0 comments on commit e7968b4

Please sign in to comment.