From 0eb4720134e0e88c5f02cdf6fcd3e2de061efb89 Mon Sep 17 00:00:00 2001 From: "Deomid \"rojer\" Ryabkov" Date: Sun, 17 Oct 2021 01:47:26 +0300 Subject: [PATCH] Move some more flags to the flags package --- cli/build.go | 4 +-- cli/build_remote.go | 2 +- cli/common/paths/paths.go | 63 +++++++++++++++++++-------------------- cli/common/state/state.go | 6 ++-- cli/flags/flags.go | 7 +++++ cli/license_cmd/key.go | 9 +++--- 6 files changed, 47 insertions(+), 44 deletions(-) diff --git a/cli/build.go b/cli/build.go index 5c46c94d..450442d6 100644 --- a/cli/build.go +++ b/cli/build.go @@ -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() { diff --git a/cli/build_remote.go b/cli/build_remote.go index 8702c7be..21d2f15d 100644 --- a/cli/build_remote.go +++ b/cli/build_remote.go @@ -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) } diff --git a/cli/common/paths/paths.go b/cli/common/paths/paths.go index 360610c9..57ba5e49 100644 --- a/cli/common/paths/paths.go +++ b/cli/common/paths/paths.go @@ -18,6 +18,7 @@ package paths import ( "fmt" + "io/ioutil" "os" "path/filepath" "runtime" @@ -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" ) @@ -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) } @@ -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 } @@ -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") } diff --git a/cli/common/state/state.go b/cli/common/state/state.go index 5b0cab5c..7fba387a 100644 --- a/cli/common/state/state.go +++ b/cli/common/state/state.go @@ -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" ) @@ -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) } @@ -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) } diff --git a/cli/flags/flags.go b/cli/flags/flags.go index 9e6d6e7c..428833f5 100644 --- a/cli/flags/flags.go +++ b/cli/flags/flags.go @@ -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") diff --git a/cli/license_cmd/key.go b/cli/license_cmd/key.go index 8912edc6..be1f28ff 100644 --- a/cli/license_cmd/key.go +++ b/cli/license_cmd/key.go @@ -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" @@ -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) } @@ -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) } @@ -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 }