Skip to content

Commit

Permalink
fix: not set flag value to config
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Mar 14, 2023
1 parent de422e9 commit ef83487
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 72 deletions.
2 changes: 2 additions & 0 deletions auth/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

// todo: rm checkPermission after v1.13.0
func InitRouter(app OAuthApp, checkPermission bool) http.Handler {
gin.SetMode(gin.ReleaseMode)

router := gin.New()
router.Use(CorsMiddleWare())
router.Use(RewriteAddressInUrl())
Expand Down
87 changes: 26 additions & 61 deletions cli/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"path"

"github.com/filecoin-project/venus-auth/config"
"github.com/mitchellh/go-homedir"
"github.com/filecoin-project/venus-auth/util"
)

type Repo interface {
GetConfig() (*config.Config, error)
SaveConfig(*config.Config) error
GetToken() (string, error)
SaveToken(string) error
GetDataDir() (string, error)
GetDataDir() string
}

const (
Expand All @@ -38,24 +38,9 @@ type FsRepo struct {

func (r *FsRepo) GetConfig() (*config.Config, error) {
path := path.Join(r.repoPath, r.configPath)
exist, err := exist(path)
cnf, err := config.DecodeConfig(path)
if err != nil {
return nil, fmt.Errorf("check config exist: %w", err)
}
if exist {
cnf, err := config.DecodeConfig(path)
if err != nil {
return nil, fmt.Errorf("decode config: %w", err)
}
return cnf, nil
}
cnf, err := config.DefaultConfig()
if err != nil {
return nil, fmt.Errorf("generate secret: %w", err)
}
err = config.Cover(path, cnf)
if err != nil {
return nil, fmt.Errorf("save config: %w", err)
return nil, fmt.Errorf("decode config: %w", err)
}
return cnf, nil
}
Expand All @@ -67,7 +52,7 @@ func (r *FsRepo) SaveConfig(cnf *config.Config) error {

func (r *FsRepo) GetToken() (string, error) {
path := path.Join(r.repoPath, r.tokenPath)
exist, err := exist(path)
exist, err := util.Exist(path)
if err != nil {
return "", fmt.Errorf("check token exist: %w", err)
}
Expand All @@ -87,60 +72,40 @@ func (r *FsRepo) SaveToken(token string) error {
return os.WriteFile(path, []byte(token), os.ModePerm)
}

func (r *FsRepo) GetDataDir() (string, error) {
ret := path.Join(r.repoPath, r.dataPath)
err := makeDir(ret)
if err != nil {
return "", fmt.Errorf("make data dir: %w", err)
}
return ret, nil
func (r *FsRepo) GetDataDir() string {
return path.Join(r.repoPath, r.dataPath)
}

func NewFsRepo(repoPath string) (Repo, error) {
var err error
repoPath, err = homedir.Expand(repoPath)
func (r *FsRepo) init() error {
exist, err := util.Exist(r.repoPath)
if err != nil {
return nil, fmt.Errorf("expand home dir: %w", err)
return err
}
ret := &FsRepo{
repoPath: repoPath,
configPath: DefaultConfigFile,
dataPath: DefaultDataDir,
tokenPath: DefaultTokenFile,
if exist {
return nil
}

// create repo if not exist
err = makeDir(repoPath)
err = util.MakeDir(r.repoPath)
if err != nil {
return nil, fmt.Errorf("make repo dir: %w", err)
return fmt.Errorf("make repo dir: %w", err)
}
return ret, nil
}

func makeDir(path string) error {
fi, err := os.Stat(path)
err = util.MakeDir(r.GetDataDir())
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return fmt.Errorf("make dir: %w", err)
}
} else {
return fmt.Errorf("stat dir: %w", err)
}
} else {
if !fi.IsDir() {
return fmt.Errorf("path %s is not a dir", path)
}
return fmt.Errorf("make data dir: %w", err)
}

return nil
}

func exist(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
} else if !os.IsNotExist(err) {
return false, err
func NewFsRepo(repoPath string) (Repo, error) {
ret := &FsRepo{
repoPath: repoPath,
configPath: DefaultConfigFile,
dataPath: DefaultDataDir,
tokenPath: DefaultTokenFile,
}
return false, nil

return ret, ret.init()
}
58 changes: 47 additions & 11 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package cli
import (
"fmt"
"net/http"
"path/filepath"

"github.com/filecoin-project/venus-auth/auth"
"github.com/filecoin-project/venus-auth/config"
"github.com/filecoin-project/venus-auth/log"
"github.com/gin-gonic/gin"
"github.com/filecoin-project/venus-auth/util"
"github.com/ipfs-force-community/metrics"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
"go.opencensus.io/plugin/ochttp"
)
Expand All @@ -34,33 +37,66 @@ var runCommand = &cli.Command{
Action: run,
}

func run(cliCtx *cli.Context) error {
gin.SetMode(gin.ReleaseMode)
func configScan(path string, cliCtx *cli.Context) (*config.Config, error) {
exist, err := util.Exist(path)
if err != nil {
return nil, fmt.Errorf("failed to check file exist : %s", err)
}
if exist {
cnf, err := config.DecodeConfig(path)
if err != nil {
return nil, fmt.Errorf("failed to decode config : %s", err)
}

repoPath := cliCtx.String("repo")
repo, err := NewFsRepo(repoPath)
return fillConfigByFlag(cnf, cliCtx), nil
}

cnf, err := config.DefaultConfig()
if err != nil {
return fmt.Errorf("init repo: %s", err)
return nil, fmt.Errorf("failed to generate secret : %s", err)
}
cnf, err := repo.GetConfig()
cnf = fillConfigByFlag(cnf, cliCtx)
err = config.Cover(path, cnf)
if err != nil {
return fmt.Errorf("get config: %s", err)
return nil, fmt.Errorf("failed to write config to home dir : %s", err)
}

log.InitLog(cnf.Log)
return cnf, nil
}

func fillConfigByFlag(cnf *config.Config, cliCtx *cli.Context) *config.Config {
if cliCtx.IsSet("mysql-dsn") {
cnf.DB.DSN = cliCtx.String("mysql-dsn")
}
if cliCtx.IsSet("db-type") {
cnf.DB.Type = cliCtx.String("db-type")
}

dataPath, err := repo.GetDataDir()
return cnf
}

func run(cliCtx *cli.Context) error {
repoPath, err := homedir.Expand(cliCtx.String("repo"))
if err != nil {
return fmt.Errorf("expand home dir: %w", err)
}
repo, err := NewFsRepo(repoPath)
if err != nil {
return fmt.Errorf("init repo: %s", err)
}

cnfPath := cliCtx.String("config")
if len(cnfPath) == 0 {
cnfPath = filepath.Join(repoPath, DefaultConfigFile)
}
cnf, err := configScan(cnfPath, cliCtx)
if err != nil {
return fmt.Errorf("get data dir: %s", err)
return err
}

log.InitLog(cnf.Log)

dataPath := repo.GetDataDir()
app, err := auth.NewOAuthApp(cnf.Secret, dataPath, cnf.DB)
if err != nil {
return fmt.Errorf("init oauth app: %s", err)
Expand Down
35 changes: 35 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package util

import (
"fmt"
"os"
)

func MakeDir(path string) error {
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(path, 0o755)
if err != nil {
return fmt.Errorf("make dir: %w", err)
}
} else {
return fmt.Errorf("stat dir: %w", err)
}
} else {
if !fi.IsDir() {
return fmt.Errorf("path %s is not a dir", path)
}
}
return nil
}

func Exist(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
} else if !os.IsNotExist(err) {
return false, err
}
return false, nil
}

0 comments on commit ef83487

Please sign in to comment.