Skip to content

Commit

Permalink
Move some more flags to the flags package
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Oct 16, 2021
1 parent e5a21ca commit 0eb4720
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 44 deletions.
4 changes: 2 additions & 2 deletions cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,9 @@ func (lpr *compProviderReal) GetLibLocalPath(
}

// Check --libs-dir.
if !ok && len(paths.LibsDirFlag) > 0 {
if !ok && len(*flags.LibsDir) > 0 {
name2, _ := m.GetName2()
for _, libsDir := range paths.LibsDirFlag {
for _, libsDir := range *flags.LibsDir {
libDir := filepath.Join(libsDir, name2)
glog.V(2).Infof("%s (%s): Trying %s...", name, name2, libDir)
if fi, err := os.Stat(libDir); err == nil && fi.IsDir() {
Expand Down
2 changes: 1 addition & 1 deletion cli/build_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func buildRemote(bParams *build.BuildParams) error {

// We'll need to amend the sources significantly with all libs, so copy them
// to temporary dir first
appStagingDir, err := ioutil.TempDir(paths.TmpDir, "tmp_mos_src_")
appStagingDir, err := paths.GetTempDir("tmp_mos_src_")
if err != nil {
return errors.Trace(err)
}
Expand Down
63 changes: 30 additions & 33 deletions cli/common/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package paths

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/juju/errors"
flag "github.com/spf13/pflag"

"github.com/mongoose-os/mos/cli/flags"
"github.com/mongoose-os/mos/version"
)

Expand All @@ -34,42 +36,24 @@ var (

AppsDirTpl = fmt.Sprintf("~/.mos/apps-%s", dirTplMosVersion)

TmpDir = ""
depsDirFlag = ""
LibsDirFlag = []string{}
AppsDir = ""
modulesDirFlag = ""

StateFilepath = ""
AuthFilepath = ""
AppsDir = ""
)

func init() {
flag.StringVar(&TmpDir, "temp-dir", "~/.mos/tmp", "Directory to store temporary files")
flag.StringVar(&depsDirFlag, "deps-dir", "", "Directory to fetch libs, modules into")
flag.StringSliceVar(&LibsDirFlag, "libs-dir", []string{}, "Directory to find libs in. Can be used multiple times.")
flag.StringVar(&AppsDir, "apps-dir", AppsDirTpl, "Directory to store apps into")
flag.StringVar(&modulesDirFlag, "modules-dir", "", "Directory to store modules into")

flag.StringVar(&StateFilepath, "state-file", "~/.mos/state.json", "Where to store internal mos state")
flag.StringVar(&AuthFilepath, "auth-file", "~/.mos/auth.json", "Where to store license server auth key")
}

// Init() should be called after all flags are parsed
func Init() error {
var err error
TmpDir, err = NormalizePath(TmpDir, version.GetMosVersion())
if err != nil {
return errors.Trace(err)
}

depsDirFlag, err = NormalizePath(depsDirFlag, version.GetMosVersion())
*flags.DepsDir, err = NormalizePath(*flags.DepsDir, version.GetMosVersion())
if err != nil {
return errors.Trace(err)
}

for i, s := range LibsDirFlag {
LibsDirFlag[i], err = NormalizePath(s, version.GetMosVersion())
for i, s := range *flags.LibsDir {
(*flags.LibsDir)[i], err = NormalizePath(s, version.GetMosVersion())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -80,25 +64,21 @@ func Init() error {
return errors.Trace(err)
}

modulesDirFlag, err = NormalizePath(modulesDirFlag, version.GetMosVersion())
*flags.ModulesDir, err = NormalizePath(*flags.ModulesDir, version.GetMosVersion())
if err != nil {
return errors.Trace(err)
}

StateFilepath, err = NormalizePath(StateFilepath, version.GetMosVersion())
*flags.StateFile, err = NormalizePath(*flags.StateFile, version.GetMosVersion())
if err != nil {
return errors.Trace(err)
}

AuthFilepath, err = NormalizePath(AuthFilepath, version.GetMosVersion())
*flags.AuthFile, err = NormalizePath(*flags.AuthFile, version.GetMosVersion())
if err != nil {
return errors.Trace(err)
}

if err := os.MkdirAll(TmpDir, 0777); err != nil {
return errors.Trace(err)
}

return nil
}

Expand Down Expand Up @@ -133,16 +113,33 @@ func NormalizePath(p, version string) (string, error) {
}

func GetDepsDir(projectDir string) string {
if depsDirFlag != "" {
return depsDirFlag
if *flags.DepsDir != "" {
return *flags.DepsDir
} else {
return filepath.Join(projectDir, "deps")
}
}

func GetTempDir(subdir string) (string, error) {
dir, err := NormalizePath(*flags.TempDir, version.GetMosVersion())
if err != nil {
return "", errors.Trace(err)
}
if err = os.MkdirAll(dir, 0777); err != nil {
return "", errors.Trace(err)
}
if subdir != "" {
dir, err = ioutil.TempDir(dir, subdir)
if err != nil {
return "", errors.Trace(err)
}
}
return dir, nil
}

func GetModulesDir(projectDir string) string {
if modulesDirFlag != "" {
return modulesDirFlag
if *flags.ModulesDir != "" {
return *flags.ModulesDir
} else {
return filepath.Join(GetDepsDir(projectDir), "modules")
}
Expand Down
6 changes: 3 additions & 3 deletions cli/common/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"encoding/json"
"io/ioutil"

"github.com/mongoose-os/mos/cli/common/paths"
"github.com/mongoose-os/mos/cli/flags"

"github.com/juju/errors"
)
Expand All @@ -40,7 +40,7 @@ var (
func Init() error {
// Try to read state from file, and if it succeeds, unmarshal json from it;
// otherwise just leave state empty
if data, err := ioutil.ReadFile(paths.StateFilepath); err == nil {
if data, err := ioutil.ReadFile(*flags.StateFile); err == nil {
if err := json.Unmarshal(data, &mosState); err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func SaveState() error {
return errors.Trace(err)
}

if err := ioutil.WriteFile(paths.StateFilepath, data, 0644); err != nil {
if err := ioutil.WriteFile(*flags.StateFile, data, 0644); err != nil {
return errors.Trace(err)
}

Expand Down
7 changes: 7 additions & 0 deletions cli/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ var (
ExtraAttr = flag.StringArray("extra-attr", nil, "manifest extra attribute info to be added to ZIP")
SignKeys = flag.StringArray("sign-key", nil, "Signing private key file name. Can be used multiple times for multipl signatures.")

StateFile = flag.String("state-file", "~/.mos/state.json", "Where to store internal mos state")
AuthFile = flag.String("auth-file", "~/.mos/auth.json", "Where to store license server auth key")

// Build flags.
BuildParams = flag.String("build-params", "", "build params file")
TempDir = flag.String("temp-dir", "~/.mos/tmp", "Directory to store temporary files")
DepsDir = flag.String("deps-dir", "", "Directory to fetch libs, modules into")
LibsDir = flag.StringSlice("libs-dir", []string{}, "Directory to find libs in. Can be used multiple times.")
ModulesDir = flag.String("modules-dir", "", "Directory to store modules into")

Local = flag.Bool("local", false, "Local build.")
Clean = flag.Bool("clean", false, "Perform a clean build, wipe the previous build state")
Expand Down
9 changes: 4 additions & 5 deletions cli/license_cmd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"io/ioutil"

"github.com/juju/errors"
"github.com/mongoose-os/mos/cli/common/paths"
"github.com/mongoose-os/mos/cli/dev"
"github.com/mongoose-os/mos/cli/flags"
"github.com/mongoose-os/mos/cli/ourutil"
Expand All @@ -41,7 +40,7 @@ type authFile struct {

func readKey(server string) string {
var auth authFile
data, err := ioutil.ReadFile(paths.AuthFilepath)
data, err := ioutil.ReadFile(*flags.AuthFile)
if err == nil {
json.Unmarshal(data, &auth)
}
Expand All @@ -66,7 +65,7 @@ License server key not found.

func saveKey(server, key string) error {
var auth authFile
data, err := ioutil.ReadFile(paths.AuthFilepath)
data, err := ioutil.ReadFile(*flags.AuthFile)
if err == nil {
json.Unmarshal(data, &auth)
}
Expand All @@ -84,8 +83,8 @@ func saveKey(server, key string) error {
})
}
data, _ = json.MarshalIndent(auth, "", " ")
if err = ioutil.WriteFile(paths.AuthFilepath, data, 0600); err == nil {
ourutil.Reportf("Saved key for %s to %s", server, paths.AuthFilepath)
if err = ioutil.WriteFile(*flags.AuthFile, data, 0600); err == nil {
ourutil.Reportf("Saved key for %s to %s", server, *flags.AuthFile)
}
return err
}
Expand Down

0 comments on commit 0eb4720

Please sign in to comment.