-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
182 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,28 @@ | ||
package cli | ||
|
||
import ( | ||
"path" | ||
"fmt" | ||
|
||
"github.com/filecoin-project/venus-auth/config" | ||
"github.com/filecoin-project/venus-auth/jwtclient" | ||
"github.com/mitchellh/go-homedir" | ||
"github.com/urfave/cli/v2" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
// nolint | ||
func GetCli(ctx *cli.Context) (*jwtclient.AuthClient, error) { | ||
p, err := homedir.Expand(ctx.String("repo")) | ||
repo, err := NewFsRepo(ctx.String("repo")) | ||
if err != nil { | ||
return nil, xerrors.Errorf("could not expand home dir (repo): %w", err) | ||
return nil, fmt.Errorf("create repo: %w", err) | ||
} | ||
cnfPath, err := homedir.Expand(ctx.String("config")) | ||
|
||
cnf, err := repo.GetConfig() | ||
if err != nil { | ||
return nil, xerrors.Errorf("could not expand home dir (config): %w", err) | ||
} | ||
if len(cnfPath) == 0 { | ||
cnfPath = path.Join(p, "config.toml") | ||
return nil, fmt.Errorf("get config: %w", err) | ||
} | ||
cnf, err := config.DecodeConfig(cnfPath) | ||
|
||
token, err := repo.GetToken() | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to decode config err: %w", err) | ||
return nil, fmt.Errorf("get token: %w", err) | ||
} | ||
return jwtclient.NewAuthClient("http://localhost:"+cnf.Port, cnf.Token) | ||
|
||
return jwtclient.NewAuthClient("http://localhost:"+cnf.Port, token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/filecoin-project/venus-auth/config" | ||
"github.com/mitchellh/go-homedir" | ||
) | ||
|
||
type Repo interface { | ||
GetConfig() (*config.Config, error) | ||
SaveConfig(*config.Config) error | ||
GetToken() (string, error) | ||
SaveToken(string) error | ||
GetDataDir() (string, error) | ||
} | ||
|
||
const ( | ||
// DefaultConfigFile is the default config file name | ||
DefaultConfigFile = "config.toml" | ||
// DefaultDataDir is the default data directory name | ||
DefaultDataDir = "data" | ||
// DefaultTokenFile is the default token file name | ||
DefaultTokenFile = "token" | ||
) | ||
|
||
type FsRepo struct { | ||
repoPath string | ||
// configPath is the relative path to the config file from the repoPath | ||
configPath string | ||
// dataPath is the relative path to the data directory from the repoPath | ||
dataPath string | ||
// tokenPath is the relative path to the token file from the repoPath | ||
tokenPath string | ||
} | ||
|
||
func (r *FsRepo) GetConfig() (*config.Config, error) { | ||
path := path.Join(r.repoPath, r.configPath) | ||
exist, err := exist(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 cnf, nil | ||
} | ||
|
||
func (r *FsRepo) SaveConfig(cnf *config.Config) error { | ||
path := path.Join(r.repoPath, r.configPath) | ||
return config.Cover(path, cnf) | ||
} | ||
|
||
func (r *FsRepo) GetToken() (string, error) { | ||
path := path.Join(r.repoPath, r.tokenPath) | ||
exist, err := exist(path) | ||
if err != nil { | ||
return "", fmt.Errorf("check token exist: %w", err) | ||
} | ||
if !exist { | ||
return "", fmt.Errorf("token not exist") | ||
} | ||
token, err := os.ReadFile(path) | ||
if err != nil { | ||
return "", fmt.Errorf("read token: %w", err) | ||
} | ||
|
||
return string(token), nil | ||
} | ||
|
||
func (r *FsRepo) SaveToken(token string) error { | ||
path := path.Join(r.repoPath, r.tokenPath) | ||
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 NewFsRepo(repoPath string) (Repo, error) { | ||
afterExpand, err := homedir.Expand(repoPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("expand home dir: %w", err) | ||
} | ||
ret := &FsRepo{ | ||
repoPath: afterExpand, | ||
configPath: DefaultConfigFile, | ||
dataPath: DefaultDataDir, | ||
tokenPath: DefaultTokenFile, | ||
} | ||
return ret, nil | ||
} | ||
|
||
func makeDir(path string) error { | ||
fi, err := os.Stat(path) | ||
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 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.