Skip to content

Commit

Permalink
feat: support setting proxy and provider for setup sub-command (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Jun 27, 2023
1 parent 8f63471 commit 957c061
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 27 deletions.
55 changes: 29 additions & 26 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,52 @@ func newSetupCommand(v *viper.Viper, stdio terminal.Stdio) (cmd *cobra.Command)
RunE: opt.runE,
GroupID: configGroup.ID,
}
flags := cmd.Flags()
flags.StringVarP(&opt.proxy, "proxy", "p", "", "The proxy of GitHub")
flags.StringVarP(&opt.provider, "provider", "", "", "The provider of hd configuration")
return
}

type setupOption struct {
stdio terminal.Stdio
v *viper.Viper

proxy string
provider string
}

func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
var (
proxyGitHub string
provider string
)

logger := log.GetLoggerFromContextOrDefault(cmd)
if proxyGitHub, err = selectFromList([]string{"", "ghproxy.com", "gh.api.99988866.xyz", "mirror.ghproxy.com"},
o.v.GetString("proxy-github"),
"Select proxy-github", o.stdio); err == nil {
o.v.Set("proxy-github", proxyGitHub)
} else {
return

if o.proxy == "" {
if o.proxy, err = selectFromList([]string{"", "ghproxy.com", "gh.api.99988866.xyz", "mirror.ghproxy.com"},
o.v.GetString("proxy-github"),
"Select proxy-github", o.stdio); err != nil {
return
}
}
o.v.Set("proxy-github", o.proxy)

if provider, err = selectFromList([]string{"github", "gitee"}, o.v.GetString("provider"),
"Select provider", o.stdio); err == nil {
o.v.Set("provider", provider)
} else {
return
if o.provider == "" {
if o.provider, err = selectFromList([]string{"github", "gitee"}, o.v.GetString("provider"),
"Select provider", o.stdio); err != nil {
return
}
}
o.v.Set("provider", o.provider)

var configDir string
fetcher := &installer.DefaultFetcher{}
if configDir, err = fetcher.GetHomeDir(); err != nil {
return
}
if err = os.MkdirAll(configDir, 0750); err != nil {
err = fmt.Errorf("failed to create directory: %s, error: %v", configDir, err)
return
}
if configDir, err = fetcher.GetHomeDir(); err == nil {
if err = os.MkdirAll(configDir, 0750); err != nil {
err = fmt.Errorf("failed to create directory: %s, error: %v", configDir, err)
return
}

configPath := filepath.Join(configDir, ".config", "hd.yaml")
logger.Info("write config into:", configPath)
err = o.v.WriteConfigAs(configPath)
configPath := filepath.Join(configDir, ".config", "hd.yaml")
logger.Info("write config into:", configPath)
err = o.v.WriteConfigAs(configPath)
}
return
}

Expand Down
24 changes: 24 additions & 0 deletions cmd/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
"context"
"testing"

"github.com/AlecAivazis/survey/v2/terminal"
expect "github.com/Netflix/go-expect"
pseudotty "github.com/creack/pty"
"github.com/hinshun/vt10x"
"github.com/linuxsuren/http-downloader/pkg/log"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -65,6 +67,28 @@ func Test_newSetupCommand(t *testing.T) {
return err
})
})

t.Run("setup with given flags", func(t *testing.T) {
RunTest(t, func(c expectConsole) {
}, func(tr terminal.Stdio) error {
fs := afero.NewMemMapFs()
v := viper.New()
v.SetFs(fs)
v.Set("provider", "gitee")
v.Set("proxy-github", "gh.api.99988866.xyz")

cmd := newSetupCommand(v, tr)
assert.Equal(t, "setup", cmd.Name())
cmd.SetArgs([]string{"--proxy", "fake.com", "--provider", "fake"})
cmd.SetContext(log.NewContextWithLogger(context.Background(), 0))

err := cmd.Execute()
assert.Nil(t, err)
assert.Equal(t, "fake.com", v.GetString("proxy-github"))
assert.Equal(t, "fake", v.GetString("provider"))
return err
})
})
}

func TestSelectFromList(t *testing.T) {
Expand Down
26 changes: 25 additions & 1 deletion pkg/log/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"context"
"io"
syslog "log"
)

Expand All @@ -27,6 +28,23 @@ func (l *LevelLog) Debug(v ...any) {
}
}

// SetLevel sets the level of logger
func (l *LevelLog) SetLevel(level int) *LevelLog {
l.level = level
return l
}

// GetLevel returns the level of logger
func (l *LevelLog) GetLevel() int {
return l.level
}

// SetOutput sets the output destination for the logger.
func (l *LevelLog) SetOutput(writer io.Writer) *LevelLog {
l.Logger.SetOutput(writer)
return l
}

// LoggerContext used to get and set context value
type LoggerContext string

Expand All @@ -51,13 +69,19 @@ func GetLoggerFromContextOrDefault(aware ContextAware) (logger *LevelLog) {
if !ok {
logger = GetLogger()
}
logger.level = 3
return
}

// GetLogger returns an instance of Logger
func GetLogger() *LevelLog {
return &LevelLog{
Logger: syslog.Default(),
level: 3,
}
}

// NewContextWithLogger returns a new context with given logger level
func NewContextWithLogger(ctx context.Context, level int) context.Context {
logger := GetLogger().SetLevel(level)
return context.WithValue(ctx, LoggerContextKey, logger)
}
46 changes: 46 additions & 0 deletions pkg/log/level_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package log_test

import (
"bytes"
"context"
"testing"

"github.com/linuxsuren/http-downloader/pkg/log"
"github.com/stretchr/testify/assert"
)

func TestLogger(t *testing.T) {
t.Run("default level", func(t *testing.T) {
logger := log.GetLoggerFromContextOrDefault(&fakeContextAwareObj{})
assert.Equal(t, 3, logger.GetLevel())

logger = log.GetLoggerFromContextOrDefault(&fakeContextAwareObj{ctx: context.Background()})
assert.Equal(t, 3, logger.GetLevel())

ctx := log.NewContextWithLogger(context.Background(), 5)
logger = log.GetLoggerFromContextOrDefault(&fakeContextAwareObj{ctx: ctx})
assert.Equal(t, 5, logger.GetLevel())
})

t.Run("print in different level", func(t *testing.T) {
buf := new(bytes.Buffer)
logger := log.GetLogger().SetOutput(buf)

logger.Debug("debug")
logger.Info("info")

assert.Contains(t, buf.String(), "info")

logger.SetLevel(7)
logger.Debug("debug")
assert.Contains(t, buf.String(), "debug")
})
}

type fakeContextAwareObj struct {
ctx context.Context
}

func (f *fakeContextAwareObj) Context() context.Context {
return f.ctx
}

0 comments on commit 957c061

Please sign in to comment.