From c0f24e12784dd1bd3c7d2a196e53ff96f8d20af4 Mon Sep 17 00:00:00 2001 From: Dinesh Kumar Date: Fri, 30 Aug 2019 11:06:25 +0530 Subject: [PATCH] Fixing vet and lint --- actions/actions.go | 2 +- actions/ssh.go | 2 +- actions/ssh_instances.go | 1 - command/commands.go | 4 ++-- config/defaults.go | 19 +++++++++---------- config/loader.go | 4 ++-- gcloud/client.go | 6 +++--- gcloud/desc.go | 2 +- gcloud/instance.go | 12 ++++++------ store/store_test.go | 7 +++++-- 10 files changed, 30 insertions(+), 29 deletions(-) diff --git a/actions/actions.go b/actions/actions.go index 4f29204..584ff09 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -20,7 +20,7 @@ func MapActions(ctx context.Context, db store.DB) { login := InstanceLogin{ctx: ctx, f: db, lister: lister} srch := searcher{ctx: ctx, lister: lister, finder: db} - actions[config.SshAccess] = AddSSHKeys + actions[config.SSHAccess] = AddSSHKeys actions[config.RefreshInstances] = Refresher{ctx: ctx, store: db}.RefreshInstances actions[config.SearchPrefix] = srch.SearchInstancesPrefix actions[config.SearchRegex] = srch.SearchInstancesRegex diff --git a/actions/ssh.go b/actions/ssh.go index 9d21a1e..f0a57db 100644 --- a/actions/ssh.go +++ b/actions/ssh.go @@ -38,7 +38,7 @@ func AddSSHKeys(c gcloud.Client, args config.Args) error { fmt.Println(fmt.Errorf("describe instance errored %v", err)) return err } - keys := desc.SshKeys() + keys := desc.SSHKeys() newKey, err := readKey(args.User, args.SSHFile) if err != nil { fmt.Printf("Error adding key to instance %s err: %v\n", inst.Name, err) diff --git a/actions/ssh_instances.go b/actions/ssh_instances.go index f293a4d..0602b0f 100644 --- a/actions/ssh_instances.go +++ b/actions/ssh_instances.go @@ -17,7 +17,6 @@ type InstanceLogin struct { lister } -//TODO: login could be done as search instances and login, otherwise display func (il InstanceLogin) Login(c gcloud.Client, args config.Args) error { projs, err := il.lister.Projects(il.ctx, c) if err != nil { diff --git a/command/commands.go b/command/commands.go index 325f9e0..6bb4b04 100644 --- a/command/commands.go +++ b/command/commands.go @@ -4,10 +4,10 @@ import ( "fmt" ) -func AddSSHKeyCmd(inst, ssh_key_path string, cfg Config) gcloudCommand { +func AddSSHKeyCmd(inst, kp string, cfg Config) gcloudCommand { return gcloudCommand{ name: "gcloud", - cmd: fmt.Sprintf("compute instances add-metadata %s --metadata-from-file ssh-keys=%s", inst, ssh_key_path), + cmd: fmt.Sprintf("compute instances add-metadata %s --metadata-from-file ssh-keys=%s", inst, kp), config: cfg, } } diff --git a/config/defaults.go b/config/defaults.go index 8a7c26e..3b147b5 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -46,17 +46,16 @@ func loadDefaults(configFile string) (*Defaults, error) { return nil, err } return &appConfig, err - } else { - f, err := os.Open(configFile) - if err != nil { - return nil, fmt.Errorf("config file %s open failed with error %v", configFile, err) - } + } + f, err := os.Open(configFile) + if err != nil { + return nil, fmt.Errorf("config file %s open failed with error %v", configFile, err) + } - err = json.NewDecoder(f).Decode(&appConfig) - if err != nil { - logger.Debugf("Try removing config file %s", configFile) - return nil, fmt.Errorf("reading config file %s failed with error %v", configFile, err) - } + err = json.NewDecoder(f).Decode(&appConfig) + if err != nil { + logger.Debugf("Try removing config file %s", configFile) + return nil, fmt.Errorf("reading config file %s failed with error %v", configFile, err) } return &appConfig, nil } diff --git a/config/loader.go b/config/loader.go index 26dce84..d43a276 100644 --- a/config/loader.go +++ b/config/loader.go @@ -33,7 +33,7 @@ type Args struct { type CmdAction string -const SshAccess CmdAction = "ssh_access" +const SSHAccess CmdAction = "ssh_access" const RefreshInstances CmdAction = "refresh" const LoginInstances CmdAction = "login" const SearchPrefix CmdAction = "prefix_search" @@ -95,7 +95,7 @@ func MustLoad() { if args.Filter == "" && args.InstanceName == "" { log.Fatalf("[Config] mention instances search filter for access") } - cmdAction = SshAccess + cmdAction = SSHAccess } else if os.Args[1] == "instances" { if len(os.Args) < 3 { instanceCommand.Usage() diff --git a/gcloud/client.go b/gcloud/client.go index 48eba79..e88905b 100644 --- a/gcloud/client.go +++ b/gcloud/client.go @@ -21,8 +21,8 @@ type Client struct { executor } -//TODO: get project name as arg func (c Client) GetInstances(cfg command.Config) ([]Instance, error) { + // TODO: get project name as arg giCmd := command.GetInstancesCmd(cfg) out, err := c.Execute(giCmd) if err != nil { @@ -36,8 +36,8 @@ func (c Client) GetInstances(cfg command.Config) ([]Instance, error) { return insts, nil } -//TODO: move this to instance func (c Client) GetDescription(inst string, cfg command.Config) (Description, error) { + //TODO: move this to instance out, err := c.Execute(command.DescribeCmd(inst, cfg)) if err != nil { return Description{}, err @@ -79,8 +79,8 @@ func (c Client) ListProjects(cfg command.Config) ([]Project, error) { return projects, err } -//TODO: move to separate as it doesn't deal with gcloud func (c Client) Login(ctx context.Context, insts []Instance, cmd string, cfg command.TmuxConfig) (string, error) { + //TODO: move to separate as it doesn't deal with gcloud var hosts []string for _, inst := range insts { if inst.Status != "RUNNING" { diff --git a/gcloud/desc.go b/gcloud/desc.go index 2f4ae77..b04a29a 100644 --- a/gcloud/desc.go +++ b/gcloud/desc.go @@ -11,7 +11,7 @@ type Description struct { Metadata `json:"metadata"` } -func (d Description) SshKeys() []SSHKey { +func (d Description) SSHKeys() []SSHKey { for _, i := range d.Items { if i.Key == "ssh-keys" { return parseSSHKeys(i.Value) diff --git a/gcloud/instance.go b/gcloud/instance.go index 3dd6c90..a278971 100644 --- a/gcloud/instance.go +++ b/gcloud/instance.go @@ -14,10 +14,10 @@ type Instance struct { Project string `json:"projectID"` } -func (ins Instance) String() string { - res := fmt.Sprintf("%-30s : %-50s %-10s", ins.Project, ins.Name, ins.IP()) - if ins.ExternalIP() != "" { - return fmt.Sprintf("%s External: %s", res, ins.ExternalIP()) +func (i Instance) String() string { + res := fmt.Sprintf("%-30s : %-50s %-10s", i.Project, i.Name, i.IP()) + if i.ExternalIP() != "" { + return fmt.Sprintf("%s External: %s", res, i.ExternalIP()) } return res } @@ -43,8 +43,8 @@ type NetworkInterface struct { } type AccessConfig struct { - NatIP string `natIP` - Name string `name` + NatIP string `json:"natIP"` + Name string `json:"name"` } func createTempFile(keys []SSHKey) (*os.File, error) { diff --git a/store/store_test.go b/store/store_test.go index e4e955a..11c96a2 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -32,10 +32,13 @@ type SearchSuite struct { func (s *SearchSuite) TestSetup() { logger.SetLevel("error") - s.ctx, _ = context.WithTimeout(context.Background(), time.Second*3) + var canc context.CancelFunc + s.ctx, canc = context.WithTimeout(context.Background(), time.Second*3) + defer canc() t := s.T() var err error - s.db, err = store.NewDB("./testdata/search.db") + //TODOL should create the file and delete in teardown + s.db, err = store.NewDB("../testdata/search.db") s.bucket = "search-instances" s.instances = []gcloud.Instance{ {Name: "integration-01"},