A FAIRE.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

121 lines
2.7 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### cmdline ####### Copyright (c) 2021 mls-361 ################################################# MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package cmdline
import (
"fmt"
"os"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/mls-361/crypto"
"forge.chapril.org/mls-361/errors"
)
const _errArgs = errors.Sentinel("not enough or too many arguments")
func newCrypto(app *application.Application) (*crypto.Crypto, error) {
c := crypto.New()
key, ok := app.LookupEnv("KEY")
if !ok {
return c, nil
}
return c, c.SetKey(key)
}
func decrypt(app *application.Application) error {
if len(os.Args) != 3 {
help(app)
return _errArgs
}
es := os.Args[2]
c, err := newCrypto(app)
if err != nil {
return err
}
ds, err := c.DecryptString(es)
if err != nil {
return err
}
fmt.Println(es, "===>>", ds)
return nil
}
func encrypt(app *application.Application) error {
if len(os.Args) != 3 {
help(app)
return _errArgs
}
ds := os.Args[2]
c, err := newCrypto(app)
if err != nil {
return err
}
es, err := c.EncryptString(ds)
if err != nil {
return err
}
fmt.Println(ds, "===>>", es)
return nil
}
func version(app *application.Application) {
fmt.Println()
fmt.Println("------------------------------------------------")
fmt.Println(" application :", app.Name(), "v"+app.Version())
fmt.Println(" built at :", app.BuiltAt().Format("2006-01-02 15:04:05"))
fmt.Println(" copyright :", "mls-361")
fmt.Println(" license :", "MIT")
fmt.Println("--------------------------------------@(°_°)@---")
fmt.Println()
}
func help(app *application.Application) {
fmt.Println()
fmt.Println("------------------------------------------------")
fmt.Println(" commands:")
fmt.Println(" [-[-]]decrypt|-d|-D <string>")
fmt.Println(" [-[-]]encrypt|-e|-E <string>")
fmt.Println(" [-[-]]version|-v|-V")
fmt.Println("--------------------------------------@(°_°)@---")
fmt.Println()
}
// Parse AFAIRE.
func Parse(app *application.Application) (bool, error) {
if len(os.Args) == 1 {
return false, nil
}
switch os.Args[1] {
case "decrypt", "-decrypt", "--decrypt", "-d", "-D":
return true, decrypt(app)
case "encrypt", "-encrypt", "--encrypt", "-e", "-E":
return true, encrypt(app)
case "version", "-version", "--version", "-v", "-V":
version(app)
default:
help(app)
}
return true, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/