Skip to content

Commit

Permalink
fix: error config parse (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Jun 28, 2020
1 parent ffba879 commit 4d6fddd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 50 deletions.
8 changes: 4 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func main() {
}

func configAction(ctx *cli.Context) {
c, err := config.ParseFromRC()
if err != nil {
log.Fatalln(err)
c := config.New()
if err := c.Parse(); err == nil {
log.Println("WARN: You are modifying an existing configuration!")
}

if err := c.Prompt(); err != nil {
Expand All @@ -55,7 +55,7 @@ func configAction(ctx *cli.Context) {
}

func appAction(ctx *cli.Context) {
conf, err := config.Parse(ctx)
conf, err := config.NewWithCtx(ctx)
if err != nil {
log.Fatalln(err)
}
Expand Down
97 changes: 51 additions & 46 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -17,61 +16,37 @@ import (

// Config provides yml configuration.
type Config struct {
f *os.File

Core engine.Config `yaml:"core"`
Driver uploader.Config `yaml:"driver"`
}

func New() *Config {
return &Config{}
}

// Parse returns Config from RC file if no flags and
// returns Config from flags if any flags exist.
func Parse(ctx *cli.Context) (*Config, error) {
func NewWithCtx(ctx *cli.Context) (*Config, error) {
c := New()
if ctx.NumFlags() > 0 {
c := &Config{
Core: engine.Config{
SaveRoot: ctx.String(uploaderFlagSaveRoot),
ForceSync: true,
},
Driver: uploader.Config{
Name: ctx.String(uploaderFlagDriver),
Region: ctx.String(uploaderFlagRegion),
Bucket: ctx.String(uploaderFlagBucket),
AccessKey: ctx.String(uploaderFlagAccessKey),
SecretKey: ctx.String(uploaderFlagSecretKey),
},
c.Core = engine.Config{
SaveRoot: ctx.String(uploaderFlagSaveRoot),
ForceSync: true,
}
exclude := ctx.String(uploaderFlagExclude)
if exclude != "" {
c.Driver = uploader.Config{
Name: ctx.String(uploaderFlagDriver),
Region: ctx.String(uploaderFlagRegion),
Bucket: ctx.String(uploaderFlagBucket),
AccessKey: ctx.String(uploaderFlagAccessKey),
SecretKey: ctx.String(uploaderFlagSecretKey),
}
if exclude := ctx.String(uploaderFlagExclude); exclude != "" {
c.Core.Excludes = strings.Split(exclude, ",")
}

return c, nil
}

return ParseFromRC()
}

// ParseFromRC returns Config from rc file
func ParseFromRC() (*Config, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
} else if err := c.Parse(); err != nil {
return nil, err
}

rcPath := filepath.Join(homeDir, ".uptocrc")
f, err := os.OpenFile(rcPath, os.O_RDWR, 0644)
if os.IsNotExist(err) {
return nil, fmt.Errorf("please setup your config by run `uptoc config`")
} else if err != nil {
return nil, fmt.Errorf("open .uptocrc failed: %s", err)
}

c := &Config{f: f}
yd := yaml.NewDecoder(f)
if err := yd.Decode(c); err != nil {
return nil, fmt.Errorf("decode config failed: %s", err)
}

if strings.HasPrefix(c.Core.SaveRoot, "/") {
c.Core.SaveRoot = c.Core.SaveRoot[1:]
}
Expand All @@ -83,6 +58,16 @@ func ParseFromRC() (*Config, error) {
return c, nil
}

// Parse returns Config from rc file
func (c *Config) Parse() error {
f, err := c.open(os.O_RDONLY)
if err != nil {
return err
}

return yaml.NewDecoder(f).Decode(c)
}

// Prompt implement a prompt for the config
func (c *Config) Prompt() error {
prompts := []struct {
Expand Down Expand Up @@ -115,7 +100,27 @@ func (c *Config) Prompt() error {
*prompt.value = value
}

defer c.f.Close()
c.f.Seek(0, io.SeekStart)
return yaml.NewEncoder(c.f).Encode(c)
f, err := c.open(os.O_CREATE | os.O_WRONLY)
if err != nil {
return err
}

return yaml.NewEncoder(f).Encode(c)
}

func (c *Config) open(flag int) (*os.File, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}

rcPath := filepath.Join(homeDir, ".uptocrc")
f, err := os.OpenFile(rcPath, flag, 0644)
if os.IsNotExist(err) {
return nil, fmt.Errorf("please setup your config by run `uptoc config`")
} else if err != nil {
return nil, fmt.Errorf("open .uptocrc failed: %s", err)
}

return f, err
}

0 comments on commit 4d6fddd

Please sign in to comment.