Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added env 'OCI_REG_PLAIN_HTTP' as a switch to support OCI Registry under http protocol #140

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
1 change: 1 addition & 0 deletions pkg/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/package/test_data/test_data_add_deps/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
1 change: 1 addition & 0 deletions pkg/package/test_data/test_init_empty_mod/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
4 changes: 4 additions & 0 deletions pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -54,6 +57,7 @@ const (
FailedCreateStorePath
FailedGetPkg
UnKnownPullWhat
UnknownEnv
InvalidKclPkg
FailedUntarKclPkg
Bug
Expand Down
60 changes: 53 additions & 7 deletions pkg/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"

Expand All @@ -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()'
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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/'
Expand Down Expand Up @@ -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',
Expand Down
19 changes: 18 additions & 1 deletion pkg/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/settings/test_data/expected.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"DefaultOciRegistry":"ghcr.io","DefaultOciRepo":"kcl-lang"}
{"DefaultOciPlainHttp":false,"DefaultOciRegistry":"ghcr.io","DefaultOciRepo":"kcl-lang"}