From 6cbd021e6d089b87182fe97373fe7c7ee44310e1 Mon Sep 17 00:00:00 2001 From: j3ssie Date: Sat, 16 Sep 2023 12:02:22 +0700 Subject: [PATCH] Update git@github.com:j3ssie/osmedeus.git --- cmd/health.go | 1 + core/config.go | 2 +- core/reference.go | 1 + core/runtime.go | 10 ++++++++++ core/token.go | 44 ++++++++++++++++++++++++++++++++------------ execution/git.go | 3 ++- 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/cmd/health.go b/cmd/health.go index cbac21c..daa032a 100644 --- a/cmd/health.go +++ b/cmd/health.go @@ -88,6 +88,7 @@ func runHealth(_ *cobra.Command, args []string) error { return nil } fmt.Printf(color.GreenString("\n🦾 Everything is in order. Happy Hacking 🦾\n")) + fmt.Printf("💡 Refer to this page if you're uncertain about which workflow to run: %v\n", color.GreenString("https://docs.osmedeus.org/installation/running-your-first-scan/ ")) return nil } diff --git a/core/config.go b/core/config.go index f86ffac..e1fd8fd 100644 --- a/core/config.go +++ b/core/config.go @@ -137,7 +137,6 @@ func InitConfig(options *libs.Options) error { utils.BlockF("fatal", "Make sure you are login as 'root user' if your installation done via root user") os.Exit(-1) } - SetupOSEnv(options) return nil } @@ -162,6 +161,7 @@ func ParsingConfig(options *libs.Options) { SetupOpt(options) // get the config for cloud provider GetCloud(options) + SetupOSEnv(options) } // GetEnv get environment options diff --git a/core/reference.go b/core/reference.go index 9d8a7da..1c4983a 100644 --- a/core/reference.go +++ b/core/reference.go @@ -53,6 +53,7 @@ const ( DeleteFolder = "DeleteFolder" SplitFileByPart = "SplitFileByPart" FileLength = "FileLength" + FolderLength = "FolderLength" IsFile = "IsFile" EmptyDir = "EmptyDir" EmptyFile = "EmptyFile" diff --git a/core/runtime.go b/core/runtime.go index 0c5db00..7ab7033 100644 --- a/core/runtime.go +++ b/core/runtime.go @@ -182,6 +182,16 @@ func (r *Runner) LoadScripts() string { return otto.Value{} }) + vm.Set(FolderLength, func(call otto.FunctionCall) otto.Value { + data := utils.FolderLength(call.Argument(0).String()) + utils.DebugF("FolderLength -- %v", data) + result, err := vm.ToValue(data) + if err == nil { + return result + } + return otto.Value{} + }) + vm.Set(IsFile, func(call otto.FunctionCall) otto.Value { data := utils.FileLength(call.Argument(0).String()) var validate bool diff --git a/core/token.go b/core/token.go index 456671b..b976ebc 100644 --- a/core/token.go +++ b/core/token.go @@ -14,7 +14,7 @@ import ( ) func SetupOSEnv(options *libs.Options) { - utils.DebugF("Loading all environment variables from: %v", options.TokenConfigFile) + utils.DebugF("Loading all environment variables from: %v", color.HiGreenString(options.TokenConfigFile)) v = viper.New() v.SetConfigName("osm-var") v.SetConfigType("yaml") @@ -23,6 +23,7 @@ func SetupOSEnv(options *libs.Options) { // Read the configuration file err := v.ReadInConfig() if err != nil { + utils.ErrorF("Error reading config file: %s", err) v.SetDefault("Storages", map[string]string{ // path of secret key for push result // ~/.osmedeus/secret/storages_key @@ -92,7 +93,7 @@ func SetupOSEnv(options *libs.Options) { if ok := v.WriteConfigAs(options.TokenConfigFile); ok != nil { utils.ErrorF("Error writing config file: %s", ok) } - utils.InforF("Created a new token configuration file at %s", color.HiCyanString(options.ConfigFile)) + utils.InforF("Created a new token configuration file at %s", color.HiCyanString(options.TokenConfigFile)) } @@ -125,20 +126,35 @@ func SetupOSEnv(options *libs.Options) { } } } + // get all the config that need to be set manually - GetStorages(options) + utils.DebugF("Getting all the config that need to be set manually") GetStorages(options) GetNotification(options) GetGit(options) GetCdn(options) } +func LoadTokenFile(options *libs.Options) *viper.Viper { + + v = viper.New() + v.SetConfigName("osm-var") + v.SetConfigType("yaml") + v.AddConfigPath(path.Dir(options.TokenConfigFile)) + if err := v.ReadInConfig(); err != nil { + utils.ErrorF("Error reading config file, %s", err) + } + return v +} + func GetStorages(options *libs.Options) { - if !options.PremiumPackage || utils.GetOSEnv("ENABLE_GIT_STORAGES", "") != "TRUE" { + if !options.PremiumPackage { return } - storages := v.GetStringMapString("Storages") + utils.DebugF("Loading git storages from: %v", color.HiGreenString(options.TokenConfigFile)) + v = LoadTokenFile(options) + storages := v.GetStringMapString("storages") // get variables from config.yaml storagesOptions := make(map[string]string) @@ -148,6 +164,7 @@ func GetStorages(options *libs.Options) { } secretKey := storages["secret_key"] if secretKey == "" || secretKey == "SECRET_KEY" { + utils.DebugF("No secret key has been set. Quitting the storages setup") return } @@ -241,12 +258,13 @@ func GetStorages(options *libs.Options) { // GetNotification get storge repos func GetNotification(options *libs.Options) { - noti := v.GetStringMapString("Notification") - tokens := v.GetStringMapString("Tokens") + v = LoadTokenFile(options) + noti := v.GetStringMapString("notification") + tokens := v.GetStringMapString("tokens") // tokens - options.Noti.SlackToken = tokens["slack"] - options.Noti.TelegramToken = tokens["telegram"] + options.Noti.SlackToken = tokens["slack_api_token"] + options.Noti.TelegramToken = tokens["telegram_api_token"] // this mean you're not setup the notification yet if len(options.Noti.TelegramToken) < 20 { @@ -254,7 +272,6 @@ func GetNotification(options *libs.Options) { } options.Noti.ClientName = noti["client_name"] - options.Noti.SlackStatusChannel = noti["slack_status_channel"] options.Noti.SlackReportChannel = noti["slack_report_channel"] options.Noti.SlackDiffChannel = noti["slack_diff_channel"] @@ -269,7 +286,9 @@ func GetNotification(options *libs.Options) { // GetCdn get options for client func GetCdn(options *libs.Options) { - cdn := v.GetStringMapString("Cdn") + v = LoadTokenFile(options) + + cdn := v.GetStringMapString("cdn") options.Cdn.Bucket = cdn["cdn_s3_bucket"] options.Cdn.AccessKeyId = cdn["cdn_aws_access_key"] options.Cdn.SecretKey = cdn["cdn_aws_secret_key"] @@ -278,7 +297,8 @@ func GetCdn(options *libs.Options) { // GetGit get options for client func GetGit(options *libs.Options) { - git := v.GetStringMapString("Git") + v = LoadTokenFile(options) + git := v.GetStringMapString("git") options.Git.BaseURL = git["base_url"] options.Git.DeStorage = git["destorage"] options.Git.Token = git["api"] diff --git a/execution/git.go b/execution/git.go index 03f7206..620b248 100644 --- a/execution/git.go +++ b/execution/git.go @@ -156,13 +156,14 @@ func CloneRepo(url string, dest string, options libs.Options) error { // cloning new cloneCmd := fmt.Sprintf("GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no -i %v' git clone --depth=1 %v %v", options.Storages["secret_key"], url, dest) - Execution(cloneCmd, options) + out := utils.RunCmdWithOutput(cloneCmd) if utils.FolderExists(dest) { if utils.FileExists(path.Join(dest, "/.git/HEAD")) { return nil } return nil } + utils.DebugF(out) return errors.New("fail to clone Repo") }