ezshare/actions/misc.go
2021-12-08 05:42:25 +01:00

48 lines
1.0 KiB
Go

package actions
import (
"fmt"
"gitea.benny.dog/torjus/ezshare/certs"
"gitea.benny.dog/torjus/ezshare/config"
"github.com/urfave/cli/v2"
)
// TODO: This should probably be in some more sensible package
const Version = "v0.1.1"
func ActionGencerts(c *cli.Context) error {
outDir := "."
if c.IsSet("out-dir") {
outDir = c.String("out-dir")
}
if !c.IsSet("hostname") {
return fmt.Errorf("--hostname required")
}
hostname := c.String("hostname")
return certs.GenAllCerts(outDir, hostname)
}
func ActionInitConfig(c *cli.Context) error {
defaultCfg := config.FromDefault()
return defaultCfg.ToDefaultFile()
}
func getConfig(c *cli.Context) (*config.Config, error) {
if c.IsSet("config") {
cfgPath := c.String("config")
return config.FromFile(cfgPath)
}
cfg, err := config.FromDefaultLocations()
if err == nil {
verbosePrint(c, fmt.Sprintf("Config loaded from %s", cfg.Location()))
}
return cfg, err
}
func verbosePrint(c *cli.Context, message string) {
if c.Bool("verbose") {
fmt.Println(message)
}
}