5 changed files with 146 additions and 75 deletions
@ -0,0 +1,118 @@
|
||||
/* |
||||
------------------------------------------------------------------------------------------------------------------------ |
||||
####### 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" |
||||
) |
||||
|
||||
func getCrypto(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 nil |
||||
} |
||||
|
||||
es := os.Args[2] |
||||
|
||||
c, err := getCrypto(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 nil |
||||
} |
||||
|
||||
ds := os.Args[2] |
||||
|
||||
c, err := getCrypto(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().String()) |
||||
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 |
||||
} |
||||
|
||||
/* |
||||
######################################################################################################## @(°_°)@ ####### |
||||
*/ |
Loading…
Reference in new issue