diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index 049a15f6..15d68e3b 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -50,3 +50,4 @@ var InvalidVersionFormat = errors.New("kpm: failed to parse version.") var InvalidPkg = errors.New("invalid kcl package.") var InvalidOciUrl = errors.New("invalid oci url.") +var UnknownEnv = errors.New("invalid environment variable.") diff --git a/pkg/oci/oci.go b/pkg/oci/oci.go index 3b3ad64b..043bb141 100644 --- a/pkg/oci/oci.go +++ b/pkg/oci/oci.go @@ -93,6 +93,7 @@ func NewOciClient(regName, repoName string) (*OciClient, error) { if err != nil { return nil, settings.ErrorEvent } + repo.PlainHTTP = settings.DefaultOciPlainHttp() // Login credential, err := loadCredential(regName, settings) diff --git a/pkg/package/test_data/test_data_add_deps/main.k b/pkg/package/test_data/test_data_add_deps/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/package/test_data/test_data_add_deps/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/package/test_data/test_init_empty_mod/main.k b/pkg/package/test_data/test_init_empty_mod/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/package/test_data/test_init_empty_mod/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/reporter/reporter.go b/pkg/reporter/reporter.go index 1f200156..84183d8f 100644 --- a/pkg/reporter/reporter.go +++ b/pkg/reporter/reporter.go @@ -8,11 +8,14 @@ import ( "log" "os" "strings" + + "github.com/sirupsen/logrus" ) // Init the log. func InitReporter() { log.SetFlags(0) + logrus.SetLevel(logrus.ErrorLevel) } // Report prints to the logger. @@ -54,6 +57,7 @@ const ( FailedCreateStorePath FailedGetPkg UnKnownPullWhat + UnknownEnv InvalidKclPkg FailedUntarKclPkg Bug diff --git a/pkg/settings/settings.go b/pkg/settings/settings.go index 9657eeb6..a9f5c264 100644 --- a/pkg/settings/settings.go +++ b/pkg/settings/settings.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "sync" "time" @@ -26,14 +27,19 @@ const PACKAGE_CACHE_PATH = ".kpm/config/package-cache" // The kpm configuration type KpmConf struct { - DefaultOciRegistry string - DefaultOciRepo string + DefaultOciRegistry string + DefaultOciRepo string + DefaultOciPlainHttp bool } +const ON = "on" +const OFF = "off" const DEFAULT_REGISTRY = "ghcr.io" const DEFAULT_REPO = "kcl-lang" +const DEFAULT_OCI_PLAIN_HTTP = OFF const DEFAULT_REGISTRY_ENV = "KPM_REG" const DEFAULT_REPO_ENV = "KPM_REPO" +const DEFAULT_OCI_PLAIN_HTTP_ENV = "OCI_REG_PLAIN_HTTP" // This is a singleton that loads kpm settings from 'kpm.json' // and is only initialized on the first call by 'Init()' or 'GetSettings()' @@ -43,8 +49,9 @@ var once sync.Once // DefaultKpmConf create a default configuration for kpm. func DefaultKpmConf() KpmConf { return KpmConf{ - DefaultOciRegistry: DEFAULT_REGISTRY, - DefaultOciRepo: DEFAULT_REPO, + DefaultOciRegistry: DEFAULT_REGISTRY, + DefaultOciRepo: DEFAULT_REPO, + DefaultOciPlainHttp: DEFAULT_OCI_PLAIN_HTTP == ON, } } @@ -123,13 +130,18 @@ func (settings *Settings) DefaultOciRepo() string { return settings.Conf.DefaultOciRepo } +// DefaultOciPlainHttp return the default OCI plain http 'false'. +func (settings *Settings) DefaultOciPlainHttp() bool { + return settings.Conf.DefaultOciPlainHttp +} + // DefaultOciRef return the default OCI ref 'ghcr.io/kcl-lang'. func (settings *Settings) DefaultOciRef() string { return utils.JoinPath(settings.Conf.DefaultOciRegistry, settings.Conf.DefaultOciRepo) } // LoadSettingsFromEnv will load the kpm settings from environment variables. -func (settings *Settings) LoadSettingsFromEnv() *Settings { +func (settings *Settings) LoadSettingsFromEnv() (*Settings, *reporter.KpmEvent) { // Load the env KPM_REG reg := os.Getenv(DEFAULT_REGISTRY_ENV) if len(reg) > 0 { @@ -140,7 +152,34 @@ func (settings *Settings) LoadSettingsFromEnv() *Settings { if len(repo) > 0 { settings.Conf.DefaultOciRepo = repo } - return settings + + // Load the env OCI_REG_PLAIN_HTTP + plainHttp := os.Getenv(DEFAULT_OCI_PLAIN_HTTP_ENV) + var err *reporter.KpmEvent + if len(plainHttp) > 0 { + settings.Conf.DefaultOciPlainHttp, err = isOn(plainHttp) + if err != (*reporter.KpmEvent)(nil) { + return settings, reporter.NewErrorEvent( + reporter.UnknownEnv, + err, + fmt.Sprintf("unknown environment variable '%s=%s'", DEFAULT_OCI_PLAIN_HTTP_ENV, plainHttp), + ) + } + } + return settings, nil +} + +func isOn(input string) (bool, *reporter.KpmEvent) { + if strings.ToLower(input) == ON { + return true, nil + } else if strings.ToLower(input) == OFF { + return false, nil + } else { + return false, reporter.NewErrorEvent( + reporter.UnknownEnv, + errors.UnknownEnv, + ) + } } // GetFullPath returns the full path file path under '$HOME/.kpm/config/' @@ -228,7 +267,14 @@ func GetSettings() *Settings { kpm_settings.PackageCacheLock = flock.New(lockPath) }) - return kpm_settings.LoadSettingsFromEnv() + kpm_settings, err := kpm_settings.LoadSettingsFromEnv() + if err != (*reporter.KpmEvent)(nil) { + kpm_settings.ErrorEvent = err + } else { + kpm_settings.ErrorEvent = nil + } + + return kpm_settings } // loadOrCreateDefaultKpmJson will load the 'kpm.json' file from '$KCL_PKG_PATH/.kpm/config', diff --git a/pkg/settings/settings_test.go b/pkg/settings/settings_test.go index b57c0872..3991e601 100644 --- a/pkg/settings/settings_test.go +++ b/pkg/settings/settings_test.go @@ -77,7 +77,8 @@ func TestLoadOrCreateDefaultKpmJson(t *testing.T) { var got interface{} err = json.Unmarshal(gotJson, &got) assert.Equal(t, err, nil) - + fmt.Printf("got: %v\n", got) + fmt.Printf("expected: %v\n", expected) assert.Equal(t, reflect.DeepEqual(expected, got), true) os.RemoveAll(kpmPath) @@ -160,13 +161,29 @@ func TestSettingEnv(t *testing.T) { settings := GetSettings() assert.Equal(t, settings.DefaultOciRegistry(), "ghcr.io") assert.Equal(t, settings.DefaultOciRepo(), "kcl-lang") + assert.Equal(t, settings.DefaultOciPlainHttp(), false) err := os.Setenv("KPM_REG", "test_reg") assert.Equal(t, err, nil) err = os.Setenv("KPM_REPO", "test_repo") assert.Equal(t, err, nil) + err = os.Setenv("OCI_REG_PLAIN_HTTP", "true") + assert.Equal(t, err, nil) settings = GetSettings() assert.Equal(t, settings.DefaultOciRegistry(), "test_reg") assert.Equal(t, settings.DefaultOciRepo(), "test_repo") + assert.Equal(t, settings.ErrorEvent.Type(), reporter.UnknownEnv) + assert.Equal(t, settings.ErrorEvent.Error(), "kpm: unknown environment variable 'OCI_REG_PLAIN_HTTP=true'\nkpm: invalid environment variable.\n\n") + assert.Equal(t, settings.DefaultOciPlainHttp(), false) + + err = os.Setenv("OCI_REG_PLAIN_HTTP", "on") + assert.Equal(t, err, nil) + settings = GetSettings() + assert.Equal(t, settings.DefaultOciPlainHttp(), true) + + err = os.Setenv("OCI_REG_PLAIN_HTTP", "off") + assert.Equal(t, err, nil) + settings = GetSettings() + assert.Equal(t, settings.DefaultOciPlainHttp(), false) } diff --git a/pkg/settings/test_data/expected.json b/pkg/settings/test_data/expected.json index c2aa9981..472affcd 100644 --- a/pkg/settings/test_data/expected.json +++ b/pkg/settings/test_data/expected.json @@ -1 +1 @@ -{"DefaultOciRegistry":"ghcr.io","DefaultOciRepo":"kcl-lang"} \ No newline at end of file +{"DefaultOciPlainHttp":false,"DefaultOciRegistry":"ghcr.io","DefaultOciRepo":"kcl-lang"} \ No newline at end of file