Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Jul 11, 2022
1 parent 33b9df5 commit 9a73f18
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
34 changes: 34 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package cmd

import (
"fmt"

"github.com/clivern/goenv/core/module"

"github.com/spf13/cobra"
)

var configCmd = &cobra.Command{
Use: "config",
Short: "Configure the goenv application.",
Run: func(cmd *cobra.Command, args []string) {
golang := module.NewGolangEnvironment(HOME)

err := golang.Configure()

if err != nil {
fmt.Println(err.Error())
return
}

fmt.Println("Goenv configured successfully!")
},
}

func init() {
rootCmd.AddCommand(configCmd)
}
49 changes: 48 additions & 1 deletion cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package cmd

import (
"fmt"
"os"

"github.com/clivern/goenv/core/module"

"github.com/spf13/cobra"
)
Expand All @@ -14,7 +17,51 @@ var localCmd = &cobra.Command{
Use: "local",
Short: "Set or show the local application-specific go version.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("...")

golang := module.NewGolangEnvironment(HOME)

if len(args) == 1 {

if !golang.ValidateVersion(args[0]) {
fmt.Printf("Error! Invalid version provided %s\n", args[0])
return
}

// Set the local version
err := golang.SetLocalVersion(args[0])

if err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("Local version updated into %s\n", args[0])
}

return
}

cdir, err := os.Getwd()

if err != nil {
fmt.Println(err.Error())
}

version, err := golang.GetLocalVersion(cdir)

if err == nil {
fmt.Println(version)
return
}

fmt.Println("Unable to find local version, fallback into global version")

version, err = golang.GetGlobalVersion()

if err == nil {
fmt.Println(version)
return
}

fmt.Println(err.Error())
},
}

Expand Down
30 changes: 29 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package cmd

import (
"fmt"
"os"

"github.com/clivern/goenv/core/module"

"github.com/spf13/cobra"
)
Expand All @@ -14,7 +17,32 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Show the current go version.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("...")

golang := module.NewGolangEnvironment(HOME)

cdir, err := os.Getwd()

if err != nil {
fmt.Println(err.Error())
}

version, err := golang.GetLocalVersion(cdir)

if err == nil {
fmt.Println(version)
return
}

fmt.Println("Unable to find local version, fallback into global version")

version, err = golang.GetGlobalVersion()

if err == nil {
fmt.Println(version)
return
}

fmt.Println(err.Error())
},
}

Expand Down
19 changes: 19 additions & 0 deletions core/module/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package module

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -173,6 +174,24 @@ func (g *Golang) SetGlobalVersion(version string) error {
return g.SetVersion(path, version)
}

// SetLocalVersion sets the local golang version
func (g *Golang) SetLocalVersion(version string) error {

cdir, err := os.Getwd()

if err != nil {
return err
}

path := fmt.Sprintf(
"%s/%s",
g.FileSystem.RemoveTrailingSlash(cdir),
g.VersionFile,
)

return g.SetVersion(path, version)
}

// GetLocalVersion returns the local golang version
func (g *Golang) GetLocalVersion(dir string) (string, error) {

Expand Down

0 comments on commit 9a73f18

Please sign in to comment.