some cleanup

This commit is contained in:
Lilian Jónsdóttir 2024-03-21 10:09:05 -07:00
parent 37601be04f
commit 9c0c3ee788

View file

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"strings" "strings"
"time" "time"
@ -17,10 +18,10 @@ import (
) )
const ( const (
name = "agedit" name string = "agedit"
usage = "Edit age encrypted files with your $EDITOR" usage string = "Edit age encrypted files with your $EDITOR"
version = "0.0.2" version string = "0.0.2"
help_template = `NAME: help_template string = `NAME:
{{.Name}} {{if .Version}}v{{.Version}}{{end}} - {{.Usage}} {{.Name}} {{if .Version}}v{{.Version}}{{end}} - {{.Usage}}
USAGE: USAGE:
@ -47,7 +48,7 @@ var (
flags = []cli.Flag{ flags = []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "identity", Name: "identity",
Usage: "age identity file to use", Usage: "age identity file to decrypt with",
Aliases: []string{"i"}, Aliases: []string{"i"},
Action: func(ctx *cli.Context, identity_file string) error { Action: func(ctx *cli.Context, identity_file string) error {
if identity_file != "" { if identity_file != "" {
@ -99,23 +100,22 @@ var (
func before(ctx *cli.Context) error { func before(ctx *cli.Context) error {
// check input // check input
if input_file = strings.Join(ctx.Args().Slice(), " "); input_file == "" { if input_file = strings.Join(ctx.Args().Slice(), " "); input_file == "" {
return errors.New("no file to edit, use agedit -h for help") return fmt.Errorf("no file to edit, use " + name + " -h for help")
} }
// do some setup // do some setup
cfg = config.Defaults cfg = config.Defaults
cfg.Editor = env.GetEditor() cfg.Editor = env.GetEditor()
cfg_dir := env.GetConfigDir("agedit") cfg_dir := env.GetConfigDir(name)
cfg.IdentityFile = cfg_dir + "identity.key" cfg.IdentityFile = cfg_dir + "identity.key"
configFile = cfg_dir + "agedit.yaml" configFile = cfg_dir + name + ".yaml"
logger = log.NewWithOptions(os.Stderr, log.Options{ logger = log.NewWithOptions(os.Stderr, log.Options{
ReportTimestamp: true, ReportTimestamp: true,
TimeFormat: time.TimeOnly, TimeFormat: time.TimeOnly,
}) })
// load config from file // load config from file
_, err := os.Open(configFile) if _, err := os.Stat(configFile); err != nil && errors.Is(err, os.ErrNotExist) {
if err != nil && errors.Is(err, os.ErrNotExist) {
// or not // or not
logger.Debug("couldn't load config file", "file", configFile) logger.Debug("couldn't load config file", "file", configFile)
} else { } else {
@ -130,7 +130,7 @@ func before(ctx *cli.Context) error {
// action does the actual thing // action does the actual thing
func action(ctx *cli.Context) error { func action(ctx *cli.Context) error {
if _, err := os.Open(input_file); os.IsNotExist(err) { if _, err := os.Stat(input_file); os.IsNotExist(err) {
return err return err
} }
@ -139,8 +139,8 @@ func action(ctx *cli.Context) error {
logger.Debug("out file not specified, using input", "outfile", output_file) logger.Debug("out file not specified, using input", "outfile", output_file)
} }
if _, err := os.Open(cfg.IdentityFile); os.IsNotExist(err) { if _, err := os.Stat(cfg.IdentityFile); os.IsNotExist(err) {
return errors.New("identity file unset, use -i or set one in the config file") return fmt.Errorf("identity file unset, use -i or set one in the config file")
} }
if id, err := encrypt.ReadIdentityFromFile(cfg.IdentityFile); err != nil { if id, err := encrypt.ReadIdentityFromFile(cfg.IdentityFile); err != nil {