Merge pull request #4103 from filecoin-project/asr/config

Add an option to set config
This commit is contained in:
Aayush Rajasekaran 2020-09-30 03:33:52 -04:00 committed by GitHub
commit c77ab47cb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 14 deletions

View File

@ -127,6 +127,10 @@ var DaemonCmd = &cli.Command{
Usage: "manage open file limit", Usage: "manage open file limit",
Value: true, Value: true,
}, },
&cli.StringFlag{
Name: "config",
Usage: "specify path of config file to use",
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
err := runmetrics.Enable(runmetrics.RunMetricOptions{ err := runmetrics.Enable(runmetrics.RunMetricOptions{
@ -180,6 +184,10 @@ var DaemonCmd = &cli.Command{
return xerrors.Errorf("opening fs repo: %w", err) return xerrors.Errorf("opening fs repo: %w", err)
} }
if cctx.String("config") != "" {
r.SetConfigPath(cctx.String("config"))
}
if err := r.Init(repo.FullNode); err != nil && err != repo.ErrRepoExists { if err := r.Init(repo.FullNode); err != nil && err != repo.ErrRepoExists {
return xerrors.Errorf("repo init error: %w", err) return xerrors.Errorf("repo init error: %w", err)
} }

View File

@ -66,6 +66,7 @@ var ErrRepoExists = xerrors.New("repo exists")
// FsRepo is struct for repo, use NewFS to create // FsRepo is struct for repo, use NewFS to create
type FsRepo struct { type FsRepo struct {
path string path string
configPath string
} }
var _ Repo = &FsRepo{} var _ Repo = &FsRepo{}
@ -79,9 +80,14 @@ func NewFS(path string) (*FsRepo, error) {
return &FsRepo{ return &FsRepo{
path: path, path: path,
configPath: filepath.Join(path, fsConfig),
}, nil }, nil
} }
func (fsr *FsRepo) SetConfigPath(cfgPath string) {
fsr.configPath = cfgPath
}
func (fsr *FsRepo) Exists() (bool, error) { func (fsr *FsRepo) Exists() (bool, error) {
_, err := os.Stat(filepath.Join(fsr.path, fsDatastore)) _, err := os.Stat(filepath.Join(fsr.path, fsDatastore))
notexist := os.IsNotExist(err) notexist := os.IsNotExist(err)
@ -115,9 +121,7 @@ func (fsr *FsRepo) Init(t RepoType) error {
} }
func (fsr *FsRepo) initConfig(t RepoType) error { func (fsr *FsRepo) initConfig(t RepoType) error {
cfgP := filepath.Join(fsr.path, fsConfig) _, err := os.Stat(fsr.configPath)
_, err := os.Stat(cfgP)
if err == nil { if err == nil {
// exists // exists
return nil return nil
@ -125,7 +129,7 @@ func (fsr *FsRepo) initConfig(t RepoType) error {
return err return err
} }
c, err := os.Create(cfgP) c, err := os.Create(fsr.configPath)
if err != nil { if err != nil {
return err return err
} }
@ -216,6 +220,7 @@ func (fsr *FsRepo) Lock(repoType RepoType) (LockedRepo, error) {
} }
return &fsLockedRepo{ return &fsLockedRepo{
path: fsr.path, path: fsr.path,
configPath: fsr.configPath,
repoType: repoType, repoType: repoType,
closer: closer, closer: closer,
}, nil }, nil
@ -223,6 +228,7 @@ func (fsr *FsRepo) Lock(repoType RepoType) (LockedRepo, error) {
type fsLockedRepo struct { type fsLockedRepo struct {
path string path string
configPath string
repoType RepoType repoType RepoType
closer io.Closer closer io.Closer
@ -277,7 +283,7 @@ func (fsr *fsLockedRepo) Config() (interface{}, error) {
} }
func (fsr *fsLockedRepo) loadConfigFromDisk() (interface{}, error) { func (fsr *fsLockedRepo) loadConfigFromDisk() (interface{}, error) {
return config.FromFile(fsr.join(fsConfig), defConfForType(fsr.repoType)) return config.FromFile(fsr.configPath, defConfForType(fsr.repoType))
} }
func (fsr *fsLockedRepo) SetConfig(c func(interface{})) error { func (fsr *fsLockedRepo) SetConfig(c func(interface{})) error {
@ -306,7 +312,7 @@ func (fsr *fsLockedRepo) SetConfig(c func(interface{})) error {
} }
// write buffer of TOML bytes to config file // write buffer of TOML bytes to config file
err = ioutil.WriteFile(fsr.join(fsConfig), buf.Bytes(), 0644) err = ioutil.WriteFile(fsr.configPath, buf.Bytes(), 0644)
if err != nil { if err != nil {
return err return err
} }