go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/cli/config/store.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package config 5 6 import ( 7 "os" 8 "path/filepath" 9 10 "github.com/cockroachdb/errors" 11 "github.com/rs/zerolog/log" 12 "github.com/spf13/afero" 13 "github.com/spf13/viper" 14 ) 15 16 func StoreConfig() error { 17 path := viper.ConfigFileUsed() 18 log.Info().Str("path", path).Msg("saving config") 19 20 // create new file if it does not exist 21 osFs := afero.NewOsFs() 22 if _, err := osFs.Stat(path); os.IsNotExist(err) { 23 log.Info().Str("path", path).Msg("config file does not exist, create a new one") 24 // create the directory if it does not exist 25 osFs.MkdirAll(filepath.Dir(path), 0o755) 26 27 // write file 28 err = os.WriteFile(path, []byte{}, 0o644) 29 if err != nil { 30 return errors.Wrap(err, "failed to save mondoo config") 31 } 32 } else if err != nil { 33 return errors.Wrap(err, "failed to check stats for mondoo config") 34 } 35 36 return viper.WriteConfig() 37 }