Skip to content

Commit

Permalink
Merge pull request Nordstrom#36 from tylerdavisnordstrom/login_setCon…
Browse files Browse the repository at this point in the history
…text

Fix client metalinter warnings
  • Loading branch information
SleepyBrett authored Jan 10, 2018
2 parents af573c8 + af2c689 commit 1396cfe
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/pkg/errors"

"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

type app struct {
Expand Down Expand Up @@ -52,6 +52,7 @@ type AliasConfig struct {
KubectlUser string `yaml:"kubectl-user"`
}

// Config contains the array of aliases (AliasConfig)
type Config struct {
Aliases []*AliasConfig `yaml:"aliases"`
}
Expand All @@ -61,7 +62,7 @@ func findFreePort() (string, error) {
if err != nil {
return "", err
}
defer server.Close()
defer server.Close() // nolint: errcheck
hostString := server.Addr().String()
_, portString, err := net.SplitHostPort(hostString)
if err != nil {
Expand All @@ -86,7 +87,7 @@ func (app *app) makeExchange(token string) error {
if res.StatusCode != http.StatusOK {
log.Fatalf("Failed to retrieve token from kubelogin server. Please try again or contact your administrator")
}
defer res.Body.Close()
defer res.Body.Close() // nolint: errcheck
jwt, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Unable to read response body. %s", err)
Expand All @@ -110,10 +111,7 @@ func (app *app) tokenHandler(w http.ResponseWriter, r *http.Request) {

func (app *app) configureKubectl(jwt string) error {
configCmd := exec.Command("kubectl", "config", "set-credentials", app.kubectlUser, "--token="+jwt)
if err := configCmd.Run(); err != nil {
return err
}
return nil
return configCmd.Run()
}

func (app *app) generateAuthURL() (string, string, error) {
Expand All @@ -133,7 +131,10 @@ func createMux(app app) *http.ServeMux {
newMux.HandleFunc("/exchange/", app.tokenHandler)
newMux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(""))
_, err := w.Write([]byte(""))
if err != nil {
log.Printf("Unable to write favicon: %v ", err)
}
return
})
return newMux
Expand All @@ -156,7 +157,7 @@ func generateURLAndListenForServerResponse(app app) {
fmt.Printf("Opening %s ...\n", loginURL)
err := exec.Command("/usr/bin/open", loginURL).Run()
if err != nil {
fmt.Printf("Error opening; please open the URL manually.\n", loginURL)
fmt.Printf("Error opening; please open the URL manually: %s \n", loginURL)
}
} else {
fmt.Printf("Follow this URL to log into auth provider: %s\n", loginURL)
Expand Down Expand Up @@ -214,7 +215,7 @@ func (config *Config) createConfig(onDiskFile string, aliasConfig AliasConfig) e
if err != nil {
return errors.Wrap(err, "failed to create file in root directory")
}
fh.Close()
_ = fh.Close()
}

log.Print("Config file created, setting config values...")
Expand Down Expand Up @@ -267,10 +268,7 @@ func (app *app) configureFile(kubeloginrcAlias string, loginServerURL *url.URL,
aliasConfig := config.newAliasConfig(kubeloginrcAlias, loginServerURL.String(), kubectlUser)
yamlFile, err := ioutil.ReadFile(app.filenameWithPath)
if err != nil {
if err := config.createConfig(app.filenameWithPath, aliasConfig); err != nil {
return err
}
return nil
return config.createConfig(app.filenameWithPath, aliasConfig) // Either error or nil value
}
if err := yaml.Unmarshal(yamlFile, &config); err != nil {
return errors.Wrap(err, "failed to unmarshal yaml file")
Expand All @@ -285,10 +283,8 @@ func (app *app) configureFile(kubeloginrcAlias string, loginServerURL *url.URL,
log.Print("New Alias configured")
return nil
}
if err := config.updateAlias(foundAliasConfig, loginServerURL, app.filenameWithPath); err != nil {
return err
}
return nil

return config.updateAlias(foundAliasConfig, loginServerURL, app.filenameWithPath) // Either error or nil value
}

func main() {
Expand All @@ -315,7 +311,7 @@ func main() {
}
generateURLAndListenForServerResponse(app)
} else {
loginCommmand.Parse(os.Args[2:])
_ = loginCommmand.Parse(os.Args[2:])
if loginCommmand.Parsed() {
if kubeloginServerBaseURL == "" {
log.Fatal("--server-url must be set!")
Expand All @@ -326,7 +322,7 @@ func main() {
}
}
case "config":
configCommand.Parse(os.Args[2:])
_ = configCommand.Parse(os.Args[2:])
if configCommand.Parsed() {
if kubeloginServerBaseURL == "" {
log.Fatal("--server-url must be set!")
Expand Down

0 comments on commit 1396cfe

Please sign in to comment.