Skip to content

Commit

Permalink
Linting issues (#865)
Browse files Browse the repository at this point in the history
* linting issues

Signed-off-by: Ramiro Berrelleza <rberrelleza@gmail.com>
  • Loading branch information
rberrelleza committed May 6, 2020
1 parent e3b089a commit d660a01
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 67 deletions.
2 changes: 1 addition & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func executeExec(ctx context.Context, dev *model.Dev, args []string) error {
return err
}

if len(dev.Container) == 0 {
if dev.Container == "" {
dev.Container = p.Spec.Containers[0].Name
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ to log in to a Okteto Enterprise instance running at okteto.example.com.
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if len(token) == 0 && k8Client.InCluster() {
if token == "" && k8Client.InCluster() {
return fmt.Errorf("this command is not supported without the '--token' flag from inside a pod")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/upState.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ const (
)

func (up *UpContext) updateStateFile(state upState) {
if len(up.Dev.Namespace) == 0 {
if up.Dev.Namespace == "" {
log.Info("can't update state file, namespace is empty")
}

if len(up.Dev.Name) == 0 {
if up.Dev.Name == "" {
log.Info("can't update state file, name is empty")
}

Expand Down
4 changes: 2 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ func writeDeployment(name, path string) error {
}

func getOktetoPath(ctx context.Context) (string, error) {
oktetoPath := os.Getenv("OKTETO_PATH")
if len(oktetoPath) == 0 {
oktetoPath, ok := os.LookupEnv("OKTETO_PATH")
if !ok {
oktetoPath = "/usr/local/bin/okteto"
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/analytics/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

func Test_generatedMachineID(t *testing.T) {
m := generateMachineID()
if len(m) == 0 || m == "na" {
if m == "" || m == "na" {
t.Fatalf("bad machineID: %s", m)
}

Expand Down Expand Up @@ -69,7 +69,7 @@ func Test_getTrackID(t *testing.T) {
}

trackID := getTrackID()
if len(trackID) == 0 || trackID == "na" {
if trackID == "" || trackID == "na" {
t.Fatalf("failed to get trackID: %s", trackID)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/build/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func getClientForOktetoCluster(ctx context.Context, buildKitHost string) (*clien
return nil, errors.Wrapf(err, "failed to get the token")
}

if len(okToken.Token) == 0 {
if okToken.Token == "" {
return nil, fmt.Errorf("auth token missing from token file")
}

Expand Down
18 changes: 10 additions & 8 deletions pkg/cmd/build/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func GetRepoNameWithoutTag(name string) string {
return fmt.Sprintf("%s/%s", domain, remainder[:i])
}

//GetImageTag returns the image tag to build for a given servicecs
//GetImageTag returns the image tag to build for a given services
func GetImageTag(image, service, namespace, oktetoRegistryURL string) string {
if oktetoRegistryURL != "" {
return fmt.Sprintf("%s/%s/%s:okteto", oktetoRegistryURL, namespace, service)
Expand Down Expand Up @@ -90,13 +90,13 @@ func getDockerfileWithCacheHandler(filename string) (string, error) {
defer datawriter.Flush()

userID := okteto.GetUserID()
if len(userID) == 0 {
if userID == "" {
userID = "anonymous"
}
for scanner.Scan() {
line := scanner.Text()
traslatedLine := translateCacheHandler(line, userID)
_, _ = datawriter.WriteString(traslatedLine + "\n")
translatedLine := translateCacheHandler(line, userID)
_, _ = datawriter.WriteString(translatedLine + "\n")
}
if err := scanner.Err(); err != nil {
return "", err
Expand All @@ -105,27 +105,29 @@ func getDockerfileWithCacheHandler(filename string) (string, error) {
return tmpFile.Name(), nil
}

func translateCacheHandler(input string, userID string) string {
func translateCacheHandler(input, userID string) string {
matched, err := regexp.MatchString(`^RUN.*--mount=.*type=cache`, input)
if err != nil {
return input
}

if matched {
matched, err = regexp.MatchString(`^RUN.*--mount=id=`, input)
if err != nil {
return input
}
if matched {
return strings.Replace(input, "--mount=id=", fmt.Sprintf("--mount=id=%s-", userID), -1)
return strings.ReplaceAll(input, "--mount=id=", fmt.Sprintf("--mount=id=%s-", userID))
}
matched, err = regexp.MatchString(`^RUN.*--mount=[^ ]+,id=`, input)
if err != nil {
return input
}
if matched {
return strings.Replace(input, ",id=", fmt.Sprintf(",id=%s-", userID), -1)
return strings.ReplaceAll(input, ",id=", fmt.Sprintf(",id=%s-", userID))
}
return strings.Replace(input, "--mount=", fmt.Sprintf("--mount=id=%s,", userID), -1)
return strings.ReplaceAll(input, "--mount=", fmt.Sprintf("--mount=id=%s,", userID))
}

return input
}
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

func TestGetUserHomeDir(t *testing.T) {
home := GetUserHomeDir()
if len(home) == 0 {
if home == "" {
t.Fatal("got an empty home value")
}

Expand Down
10 changes: 4 additions & 6 deletions pkg/k8s/deployments/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,9 @@ func UpdateOktetoRevision(ctx context.Context, d *appsv1.Deployment, client *kub
revision := updated.Annotations[revisionAnnotation]
if revision != "" {
d.Annotations[okLabels.RevisionAnnotation] = revision
if err := update(d, client); err != nil {
return err
}
return nil
return update(d, client)
}

select {
case <-ticker.C:
tries++
Expand Down Expand Up @@ -220,9 +218,9 @@ func UpdateDeployments(trList map[string]*model.Translation, c *kubernetes.Clien
//TranslateDevModeOff reverses the dev mode translation
func TranslateDevModeOff(d *appsv1.Deployment) (*appsv1.Deployment, error) {
trRulesJSON := getAnnotation(d.Spec.Template.GetObjectMeta(), okLabels.TranslationAnnotation)
if len(trRulesJSON) == 0 {
if trRulesJSON == "" {
dManifest := getAnnotation(d.GetObjectMeta(), oktetoDeploymentAnnotation)
if len(dManifest) == 0 {
if dManifest == "" {
log.Infof("%s/%s is not a development environment", d.Namespace, d.Name)
return d, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/k8s/deployments/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func commonTranslation(t *model.Translation) {

//GetDevContainer returns the dev container of a given deployment
func GetDevContainer(spec *apiv1.PodSpec, name string) *apiv1.Container {
if len(name) == 0 {
if name == "" {
return &spec.Containers[0]
}

Expand Down Expand Up @@ -178,7 +178,7 @@ func TranslatePodAffinity(spec *apiv1.PodSpec, name string) {

//TranslateDevContainer translates a dev container
func TranslateDevContainer(c *apiv1.Container, rule *model.TranslationRule) {
if len(rule.Image) == 0 {
if rule.Image == "" {
rule.Image = c.Image
}
c.Image = rule.Image
Expand Down
42 changes: 23 additions & 19 deletions pkg/k8s/pods/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,25 +225,29 @@ func GetDevPodUserID(ctx context.Context, dev *model.Dev, c *kubernetes.Clientse

func parseUserID(output string) int64 {
lines := strings.Split(output, "\n")
for _, line := range lines {
if !strings.HasPrefix(line, "USER:") {
log.Infof("USER entry not not found in first development environment log line: %s", line)
return -1
}
parts := strings.Split(line, ":")
if len(parts) != 2 {
log.Infof("failed to parse USER entry: %s", line)
return -1
}
result, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
log.Infof("failed to parse USER entry: %s", line)
return -1
}
return result
if len(lines) == 0 {
log.Info("development environment logs not generated. USER cannot be inferred")
return -1
}

if !strings.HasPrefix(lines[0], "USER:") {
log.Infof("USER entry not not found in first development environment log line: %s", lines[0])
return -1
}
log.Info("development environment logs not generated. USER cannot be inferred")
return -1

parts := strings.Split(lines[0], ":")
if len(parts) != 2 {
log.Infof("failed to parse USER entry: %s", lines[0])
return -1
}

result, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
log.Infof("failed to parse USER entry: %s", lines[0])
return -1
}

return result
}

//GetDevPodLogs returns the logs of the dev pod
Expand All @@ -255,7 +259,7 @@ func GetDevPodLogs(ctx context.Context, dev *model.Dev, timestamps bool, c *kube
if p == nil {
return "", errors.ErrNotFound
}
if len(dev.Container) == 0 {
if dev.Container == "" {
dev.Container = p.Spec.Containers[0].Name
}
return containerLogs(dev.Container, p, dev.Namespace, timestamps, c)
Expand Down
12 changes: 5 additions & 7 deletions pkg/model/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ type Dev struct {
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Container string `json:"container,omitempty" yaml:"container,omitempty"`
Image string `json:"image,omitempty" yaml:"image,omitempty"`
Build *BuildInfo `json:"-,omitempty" yaml:"build,omitempty"`
Push *BuildInfo `json:"-,omitempty" yaml:"push,omitempty"`
Build *BuildInfo `json:"-" yaml:"build,omitempty"`
Push *BuildInfo `json:"-" yaml:"push,omitempty"`
ImagePullPolicy apiv1.PullPolicy `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"`
Environment []EnvVar `json:"environment,omitempty" yaml:"environment,omitempty"`
Secrets []Secret `json:"secrets,omitempty" yaml:"secrets,omitempty"`
Expand Down Expand Up @@ -644,11 +644,9 @@ func (dev *Dev) ToTranslationRule(main *Dev) *TranslationRule {
for _, s := range rule.Secrets {
rule.Args = append(rule.Args, "-s", fmt.Sprintf("%s:%s", s.GetFileName(), s.RemotePath))
}
} else {
if len(dev.Command) > 0 {
rule.Command = dev.Command
rule.Args = []string{}
}
} else if len(dev.Command) > 0 {
rule.Command = dev.Command
rule.Args = []string{}
}

for _, v := range dev.Volumes {
Expand Down
8 changes: 2 additions & 6 deletions pkg/okteto/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Auth(ctx context.Context, code, url string) (*User, error) {
}

func saveAuthData(user *User, url string) error {
if len(user.GithubID) == 0 || len(user.Token) == 0 {
if user.GithubID == "" || user.Token == "" {
return fmt.Errorf("empty response")
}

Expand All @@ -121,11 +121,7 @@ func saveAuthData(user *User, url string) error {
return fmt.Errorf("bad response: %w", err)
}

if err := ioutil.WriteFile(GetCertificatePath(), d, 0600); err != nil {
return err
}

return nil
return ioutil.WriteFile(GetCertificatePath(), d, 0600)
}

func queryUser(ctx context.Context, client *graphql.Client, token string) (*q, error) {
Expand Down
7 changes: 2 additions & 5 deletions pkg/okteto/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func isConnectionError(s string) bool {
//SetKubeConfig updates a kubeconfig file with okteto cluster credentials
func SetKubeConfig(cred *Credential, kubeConfigPath, namespace, userName, clusterName string) error {
contextName := ""
if len(namespace) == 0 {
if namespace == "" {
// don't include namespace for the personal namespace
contextName = clusterName
namespace = cred.Namespace
Expand Down Expand Up @@ -145,8 +145,5 @@ func SetKubeConfig(cred *Credential, kubeConfigPath, namespace, userName, cluste

cfg.CurrentContext = contextName

if err := clientcmd.WriteToFile(*cfg, kubeConfigPath); err != nil {
return err
}
return nil
return clientcmd.WriteToFile(*cfg, kubeConfigPath)
}
6 changes: 3 additions & 3 deletions pkg/ssh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const (
identityFile = "IdentityFile"
)

func newHost(hostnames []string, comments []string) *host {
func newHost(hostnames, comments []string) *host {
return &host{
comments: comments,
hostnames: hostnames,
Expand Down Expand Up @@ -85,7 +85,7 @@ func (h *host) String() string {

}

func newParam(keyword string, args []string, comments []string) *param {
func newParam(keyword string, args, comments []string) *param {
return &param{
comments: comments,
keyword: keyword,
Expand Down Expand Up @@ -143,7 +143,7 @@ func parse(r io.Reader) (*sshConfig, error) {
for sc.Scan() {

line := strings.TrimSpace(sc.Text())
if len(line) == 0 {
if line == "" {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ssh/reverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestReverseManager_Add(t *testing.T) {
{
name: "existing",
add: model.Reverse{Local: 8080, Remote: 8081},
reverses: map[int]*reverse{8080: &reverse{forward{localAddress: ":8080", remoteAddress: ":8081"}}},
reverses: map[int]*reverse{8080: {forward{localAddress: ":8080", remoteAddress: ":8081"}}},
wantErr: true,
},
}
Expand Down

0 comments on commit d660a01

Please sign in to comment.