-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support upload files only and support prompt config (#14)
* feat: support upload the files for typora * feat: config support rcfile and flags at the same time * feat: update the command config by the prompt * feat: support save_root for the flags * chore: add saveroot for the action.yml * refactor: add comments for the export function * refactor: update the codes for the codacy * refactor: add unit test for the high coverage
- Loading branch information
Showing
18 changed files
with
705 additions
and
326 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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/manifoldco/promptui" | ||
"github.com/urfave/cli" | ||
"gopkg.in/yaml.v2" | ||
|
||
"uptoc/engine" | ||
"uptoc/uploader" | ||
) | ||
|
||
// Config provides yml configuration. | ||
type Config struct { | ||
f *os.File | ||
|
||
Core engine.Config `yaml:"core"` | ||
Driver uploader.Config `yaml:"driver"` | ||
} | ||
|
||
// 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) { | ||
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), | ||
}, | ||
} | ||
exclude := ctx.String(uploaderFlagExclude) | ||
if 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 { | ||
return nil, err | ||
} | ||
|
||
rcPath := filepath.Join(homeDir, ".uptocrc") | ||
f, err := os.OpenFile(rcPath, os.O_CREATE|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, err | ||
} | ||
|
||
if strings.HasPrefix(c.Core.SaveRoot, "/") { | ||
c.Core.SaveRoot = c.Core.SaveRoot[1:] | ||
} | ||
|
||
if !strings.HasSuffix(c.Core.VisitHost, "/") { | ||
c.Core.VisitHost += "/" | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
// Prompt implement a prompt for the config | ||
func (c *Config) Prompt() error { | ||
prompts := []struct { | ||
label string | ||
value *string | ||
mask rune | ||
validate promptui.ValidateFunc | ||
}{ | ||
{label: uploaderFlagDriver, value: &c.Driver.Name, validate: uploader.DriverValidate}, | ||
{label: uploaderFlagRegion, value: &c.Driver.Region}, | ||
{label: uploaderFlagBucket, value: &c.Driver.Bucket}, | ||
{label: uploaderFlagAccessKey, value: &c.Driver.AccessKey}, | ||
{label: uploaderFlagSecretKey, value: &c.Driver.SecretKey, mask: '*'}, | ||
{label: uploaderFlagSaveRoot, value: &c.Core.SaveRoot}, | ||
} | ||
|
||
for _, prompt := range prompts { | ||
pp := promptui.Prompt{ | ||
Label: prompt.label, | ||
Default: *prompt.value, | ||
Validate: prompt.validate, | ||
Mask: prompt.mask, | ||
} | ||
|
||
value, err := pp.Run() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*prompt.value = value | ||
} | ||
|
||
defer c.f.Close() | ||
c.f.Seek(0, io.SeekStart) | ||
return yaml.NewEncoder(c.f).Encode(c) | ||
} |
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,53 @@ | ||
package config | ||
|
||
import "github.com/urfave/cli" | ||
|
||
const ( | ||
// uploader flags | ||
uploaderFlagDriver = "driver" | ||
uploaderFlagRegion = "region" | ||
uploaderFlagAccessKey = "access_key" | ||
uploaderFlagSecretKey = "secret_key" | ||
uploaderFlagBucket = "bucket" | ||
uploaderFlagExclude = "exclude" | ||
uploaderFlagSaveRoot = "save_root" | ||
|
||
// uploader environments | ||
uploaderEnvAccessKey = "UPTOC_UPLOADER_AK" | ||
uploaderEnvSecretKey = "UPTOC_UPLOADER_SK" | ||
) | ||
|
||
// Flags defined the support flags for the cli | ||
var Flags = []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: uploaderFlagDriver, | ||
Usage: "specify cloud storage engine", | ||
Value: "oss", | ||
}, | ||
cli.StringFlag{ | ||
Name: uploaderFlagRegion, | ||
Usage: "specify region of the cloud platform", | ||
}, | ||
cli.StringFlag{ | ||
Name: uploaderFlagBucket, | ||
Usage: "specify bucket name of the cloud platform", | ||
}, | ||
cli.StringFlag{ | ||
Name: uploaderFlagAccessKey, | ||
Usage: "specify key id of the cloud platform", | ||
EnvVar: uploaderEnvAccessKey, | ||
}, | ||
cli.StringFlag{ | ||
Name: uploaderFlagSecretKey, | ||
Usage: "specify key secret of the cloud platform", | ||
EnvVar: uploaderEnvSecretKey, | ||
}, | ||
cli.StringFlag{ | ||
Name: uploaderFlagExclude, | ||
Usage: "specify exclude the given comma separated directories (example: --exclude=.cache,test)", | ||
}, | ||
cli.StringFlag{ | ||
Name: uploaderFlagSaveRoot, | ||
Usage: "specify remote directory, default is root", | ||
}, | ||
} |
Oops, something went wrong.