Skip to content

Commit

Permalink
Use cmd.Println to ensure wirght Out Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
anagrius committed Nov 30, 2018
1 parent bfd3a80 commit 523755e
Show file tree
Hide file tree
Showing 22 changed files with 96 additions and 159 deletions.
6 changes: 3 additions & 3 deletions cmd/ingest_tokens_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ use the assigned parser at ingest time.`,

table := columnize.SimpleFormat(output)

fmt.Println()
fmt.Println(table)
fmt.Println()
cmd.Println()
cmd.Println(table)
cmd.Println()

return nil
},
Expand Down
15 changes: 7 additions & 8 deletions cmd/ingest_tokens_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"fmt"
"os"

"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
Expand All @@ -26,8 +27,7 @@ func newIngestTokensListCmd() *cobra.Command {
Use: "list [flags] <repo>",
Short: "List all ingest tokens in a repository.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

Run: func(cmd *cobra.Command, args []string) {
repo := args[0]

// Get the HTTP client
Expand All @@ -36,7 +36,8 @@ func newIngestTokensListCmd() *cobra.Command {
tokens, err := client.IngestTokens().List(repo)

if err != nil {
return fmt.Errorf("Error fetching token list: %s", err)
cmd.Println(fmt.Errorf("Error fetching token list: %s", err))
os.Exit(1)
}

var output []string
Expand All @@ -48,11 +49,9 @@ func newIngestTokensListCmd() *cobra.Command {

table := columnize.SimpleFormat(output)

fmt.Println()
fmt.Println(table)
fmt.Println()

return nil
cmd.Println()
cmd.Println(table)
cmd.Println()
},
}

Expand Down
8 changes: 5 additions & 3 deletions cmd/ingest_tokens_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -27,7 +28,7 @@ func newIngestTokensRemoveCmd() *cobra.Command {
Long: `Removes the ingest token with name '<token-name>' from the repository with name '<repo>'.`,
ValidArgs: []string{"repo", "token-name"},
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
repo := args[0]
name := args[1]

Expand All @@ -37,10 +38,11 @@ func newIngestTokensRemoveCmd() *cobra.Command {
err := client.IngestTokens().Remove(repo, name)

if err != nil {
return (fmt.Errorf("Error removing ingest token: %s", err))
cmd.Println(fmt.Errorf("Error removing ingest token: %s", err))
os.Exit(1)
}

return nil
cmd.Println("User Removed")
},
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"fmt"
"os"

"github.com/humio/cli/api"
Expand All @@ -36,7 +35,7 @@ func newLicenseCmd() *cobra.Command {
return cmd
}

func printLicenseInfo(license api.LicenseData) {
func printLicenseInfo(cmd *cobra.Command, license api.LicenseData) {

data := [][]string{
[]string{"Issued At", license.IssuedAt},
Expand All @@ -49,7 +48,7 @@ func printLicenseInfo(license api.LicenseData) {
w.SetColumnSeparator(":")
w.SetColumnAlignment([]int{tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT})

fmt.Println()
cmd.Println()
w.Render()
fmt.Println()
cmd.Println()
}
8 changes: 4 additions & 4 deletions cmd/license_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func newLicenseInstallCmd() *cobra.Command {

licenseBytes, readErr := ioutil.ReadFile(filepath)
if readErr != nil {
fmt.Println(fmt.Errorf("error reading license file: %s", readErr))
cmd.Println(fmt.Errorf("error reading license file: %s", readErr))
os.Exit(1)
}

license = string(licenseBytes)
} else if license != "" {
// License set from flag
} else {
fmt.Println("Expected either an argument <filename> or flag --license=<license>.")
cmd.Println("Expected either an argument <filename> or flag --license=<license>.")
cmd.Help()
os.Exit(1)
}
Expand All @@ -52,11 +52,11 @@ func newLicenseInstallCmd() *cobra.Command {
installErr := client.License().Install(license)

if installErr != nil {
fmt.Println(fmt.Errorf("error installing license: %s", installErr))
cmd.Println(fmt.Errorf("error installing license: %s", installErr))
os.Exit(1)
}

fmt.Println("License installed")
cmd.Println("License installed")
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/license_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func newLicenseShowCmd() *cobra.Command {
license, apiErr := client.License().Get()

if apiErr != nil {
fmt.Println(fmt.Errorf("error fetching the license: %s", apiErr))
cmd.Println(fmt.Errorf("error fetching the license: %s", apiErr))
os.Exit(1)
}

printLicenseInfo(license)
printLicenseInfo(cmd, license)
},
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ In the config file already exists, the settings will be merged into the existing
} else {
prompt.Output("")
owl := "[purple]" + prompt.Owl() + "[reset]"
fmt.Print((prompt.Colorize(owl)))
cmd.Print((prompt.Colorize(owl)))
prompt.Output("")
prompt.Title("Welcome to Humio")
prompt.Output("")
Expand Down Expand Up @@ -129,22 +129,22 @@ In the config file already exists, the settings will be merged into the existing
}

prompt.Output("")
fmt.Print("==> Testing Connection...")
cmd.Print("==> Testing Connection...")

status, statusErr := client.Status()

if statusErr != nil {
fmt.Println(prompt.Colorize("[[red]Failed[reset]]"))
cmd.Println(prompt.Colorize("[[red]Failed[reset]]"))
prompt.Output("")
prompt.Error(fmt.Sprintf("Could not connect to the Humio server: %s\nIs the address connect and reachable?", statusErr))
continue
}

if status.Status != "ok" {
fmt.Println(prompt.Colorize("[[red]Failed[reset]]"))
cmd.Println(prompt.Colorize("[[red]Failed[reset]]"))
return (fmt.Errorf("The server reported that is is malfunctioning, status: %s", status.Status))
} else {
fmt.Println(prompt.Colorize("[[green]Ok[reset]]"))
cmd.Println(prompt.Colorize("[[green]Ok[reset]]"))
}

fmt.Println("")
Expand Down
5 changes: 3 additions & 2 deletions cmd/login_choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func newLoginChooseCmd() *cobra.Command {
viper.Set("token", token)

if saveErr := saveConfig(); saveErr != nil {
fmt.Println(fmt.Errorf("error saving config: %s", saveErr))
os.Exit(1)
exitOnError(cmd, saveErr, "error saving config")
}

prompt.Info(fmt.Sprintf("Switched to account: '%s'", accountName))

cmd.Println()
prompt.Output("Address: " + address)
if username != "" {
Expand Down
11 changes: 5 additions & 6 deletions cmd/output.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package cmd

import (
"fmt"

"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
)

func printTable(rows []string) {
func printTable(cmd *cobra.Command, rows []string) {

table := columnize.SimpleFormat(rows)

fmt.Println()
fmt.Println(table)
fmt.Println()
cmd.Println()
cmd.Println(table)
cmd.Println()
}

func yesNo(isTrue bool) string {
Expand Down
14 changes: 8 additions & 6 deletions cmd/parsers_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"os"

"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
Expand All @@ -29,7 +30,7 @@ func newParsersExportCmd() *cobra.Command {
Use: "export [flags] <repo> <parser>",
Short: "Export a parser <parser> in <repo> to a file.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
repo := args[0]
parserName := args[1]

Expand All @@ -42,21 +43,22 @@ func newParsersExportCmd() *cobra.Command {

parser, apiErr := client.Parsers().Get(repo, parserName)
if apiErr != nil {
return fmt.Errorf("Error fetching parsers: %s", apiErr)
cmd.Println(fmt.Errorf("Error fetching parsers: %s", apiErr))
os.Exit(1)
}

yamlData, yamlErr := yaml.Marshal(&parser)
if yamlErr != nil {
return (fmt.Errorf("Failed to serialize the parser: %s", yamlErr))
cmd.Println(fmt.Errorf("Failed to serialize the parser: %s", yamlErr))
os.Exit(1)
}
outFilePath := outputName + ".yaml"

writeErr := ioutil.WriteFile(outFilePath, yamlData, 0644)
if writeErr != nil {
return (fmt.Errorf("Error saving the parser file: %s", writeErr))
cmd.Println(fmt.Errorf("Error saving the parser file: %s", writeErr))
os.Exit(1)
}

return nil
},
}

Expand Down
28 changes: 11 additions & 17 deletions cmd/parsers_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/humio/cli/api"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,7 +60,7 @@ install from a local file or a URL, e.g.
By default 'install' will not override existing parsers with the same name.
Use the --force flag to update existing parsers with conflicting names.
`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
// Check that we got the right number of argument
// if we only got <repo> you must supply --file or --url.
if l := len(args); l == 1 {
Expand All @@ -68,25 +69,22 @@ Use the --force flag to update existing parsers with conflicting names.
} else if url != "" {
content, readErr = getUrlParser(url)
} else {
return fmt.Errorf("if you only provide repo you must specify --file or --url.")
cmd.Println(fmt.Errorf("if you only provide repo you must specify --file or --url."))
os.Exit(1)
}
} else if l := len(args); l != 2 {
return fmt.Errorf("This command takes one or two arguments: <repo> [parser]")
cmd.Println(fmt.Errorf("This command takes one or two arguments: <repo> [parser]"))
os.Exit(1)
} else {
parserName := args[1]
content, readErr = getGithubParser(parserName)
}

if readErr != nil {
return (fmt.Errorf("Failed to get parser: %s", readErr))
}
exitOnError(cmd, readErr, "Failed to load the parser")

parser := api.Parser{}
err := yaml.Unmarshal(content, &parser)

if err != nil {
return fmt.Errorf("The parser's format was invalid: %s", readErr)
}
yamlErr := yaml.Unmarshal(content, &parser)
exitOnError(cmd, yamlErr, "The parser's format was invalid")

if name != "" {
parser.Name = name
Expand All @@ -97,12 +95,8 @@ Use the --force flag to update existing parsers with conflicting names.

reposistoryName := args[0]

err = client.Parsers().Add(reposistoryName, &parser, force)
if err != nil {
return fmt.Errorf("Error installing parser: %s", err)
}

return nil
installErr := client.Parsers().Add(reposistoryName, &parser, force)
exitOnError(cmd, installErr, "error installing parser")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/parsers_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newParsersListCmd() *cobra.Command {
output = append(output, fmt.Sprintf("%v | %v", parser.Name, checkmark(!parser.IsBuiltIn)))
}

printTable(output)
printTable(cmd, output)

return nil
},
Expand Down
13 changes: 3 additions & 10 deletions cmd/parsers_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

Expand All @@ -25,19 +23,14 @@ func newParsersRemoveCmd() *cobra.Command {
Use: "remove [flags] <repo> <parser>",
Short: "Remove (uninstall) a parser from a repository.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
repo := args[0]
parser := args[1]

// Get the HTTP client
client := NewApiClient(cmd)

err := client.Parsers().Remove(repo, parser)
if err != nil {
return fmt.Errorf("Error removing parser: %s", err)
}

return nil
apiError := client.Parsers().Remove(repo, parser)
exitOnError(cmd, apiError, "Error removing parser: %s")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func formatSimpleAccount(account api.User) string {
return strings.Join(columns, " | ")
}

func printUserTable(user api.User) {
func printUserTable(cmd *cobra.Command, user api.User) {

data := [][]string{
[]string{"Username", user.Username},
Expand Down
Loading

0 comments on commit 523755e

Please sign in to comment.