Skip to content

Commit

Permalink
create env command
Browse files Browse the repository at this point in the history
To prepare project variables for terminal and for tests.
  • Loading branch information
ianic committed Aug 25, 2021
1 parent 649bce9 commit 0789a3d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/mantil/cmd/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"fmt"

"github.com/mantil-io/mantil-cli/internal/mantil"
"github.com/spf13/cobra"
)

var envCmd = &cobra.Command{
Use: "env",
Short: "Show project environment variables",
Long: `Show project environment variables
You can set environment variables in terminal with:
$ eval $(mantil env)
`,
Run: func(cmd *cobra.Command, args []string) {
url, _ := cmd.Flags().GetBool("url")
env, config := mantil.Env()
if url {
fmt.Printf("%s", config.ApiURL)
return
}
fmt.Printf("%s", env)

},
}

func init() {
envCmd.Flags().BoolP("url", "u", false, "show only project api url")
rootCmd.AddCommand(envCmd)
}
23 changes: 23 additions & 0 deletions internal/mantil/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -172,6 +173,28 @@ func FindProjectRoot(initialPath string) (string, error) {
}
}

func Env() (string, *LocalProjectConfig) {
initPath := "."
path, err := FindProjectRoot(initPath)
if err != nil {
log.Fatal(err)
}
config, err := LoadLocalConfig(path)
if err != nil {
log.Fatal(err)
}
return fmt.Sprintf(`export %s='%s'
export %s='%s'
`, EnvProjectName, config.Name,
EnvApiURL, config.ApiURL,
), config
}

const (
EnvProjectName = "MANTIL_PROJECT_NAME"
EnvApiURL = "MANTIL_API_URL"
)

func SaveToken(projectName, token string) error {
home, err := os.UserHomeDir()
if err != nil {
Expand Down

0 comments on commit 0789a3d

Please sign in to comment.