package main import ( "embed" "flag" "fmt" "os" "strings" "github.com/google/uuid" log "github.com/sirupsen/logrus" ) const VERSION = "0.0.1" const SEPARATOR = "::" const CFGPATH = "config.yaml" const GENPATH = "dist" const DFLTCOMMAND = "webservice" const KEYPATH = ".k" var CRYPTKEY []byte var DEBUGLEVEL int var ReqLog *log.Logger var PathReqLog string var f embed.FS var appDB = make(map[string]AppData) func ParseFlags() (RunFlags,error) { runFlags := RunFlags { command: os.Getenv("CVGEN_COMMAND"), cfgPath: os.Getenv("CVGEN_CFGPATH"), genPath: os.Getenv("CVGEN_GENPATH"), keyPath: os.Getenv("CVGEN_KEYPATH"), } if runFlags.command == "" { runFlags.command = DFLTCOMMAND } if runFlags.cfgPath == "" { runFlags.cfgPath = CFGPATH } if runFlags.keyPath == "" { runFlags.keyPath = KEYPATH } if runFlags.genPath == "" { runFlags.genPath = GENPATH } commandInfo := fmt.Sprintf("command to run [\n%s]", "uuid, huuid, genkey, encrypt, decrypt, gendata, webservice") flag.StringVar(&runFlags.command, "c", runFlags.command, commandInfo) flag.StringVar(&runFlags.cfgPath, "f", runFlags.cfgPath, "path to config file") flag.StringVar(&runFlags.modelName, "m", runFlags.modelName, "[gendata] model-name to generate json file") flag.StringVar(&runFlags.genPath, "g", runFlags.genPath, "[gendata] path to generate model-name.json file") flag.StringVar(&runFlags.keyPath, "k", runFlags.keyPath, "path to key file") flag.StringVar(&runFlags.text, "t", runFlags.text, "[crypt] text to encrypt/decrypt") flag.StringVar(&runFlags.url, "u", runFlags.url, "[webservice] url to open in browser") flag.StringVar(&runFlags.textPath, "p", runFlags.textPath, "[crypt] text path to encrypt/decrypt") flag.BoolVar(&runFlags.version,"v",false,"version") flag.Parse() return runFlags,nil } func run() int { runFlags, err := ParseFlags() if err != nil { log.Fatalf("Run flags parse: %v", err) } switch runFlags.command { case "genkey": ky, err := loadPath(runFlags.keyPath) if err != nil { log.Fatalf("Error load key: %v", err) } randKey, err := getKey(ky) if err != nil { log.Fatalf("Error gen key: %v", err) } fmt.Printf("%s\n", randKey) return 0 case "encrypt": var data string loadcertkey(runFlags) if len(runFlags.text) > 0 { data,err = encrypt(runFlags.text, string(CRYPTKEY)) } else if len(runFlags.textPath) > 0 { fileData, err := loadPath(runFlags.textPath) if err != nil { log.Fatalf("Error encrypt: %v", err) } data,err = encrypt(string(fileData), string(CRYPTKEY)) } fmt.Printf("%s\n", data) return 0 case "decrypt": var data string loadcertkey(runFlags) if len(runFlags.text) > 0 { data,err = decrypt(runFlags.text, string(CRYPTKEY)) } else if len(runFlags.textPath) > 0 { fileData, err := loadPath(runFlags.textPath) if err != nil { log.Fatalf("Error decrypt: %v", err) } data,err = decrypt(string(fileData), string(CRYPTKEY)) } fmt.Printf("%s\n", data) return 0 case "gendata": return gendata(runFlags) case "webservice": loadcertkey(runFlags) return webservice(runFlags) case "huuid": uuid := uuid.NewString() fmt.Printf("%s\n", uuid) return 0 case "uuid": uuid := uuid.NewString() fmt.Printf("%s\n", strings.Replace(uuid,"-","",-1)) return 0 default: fmt.Fprintln(os.Stderr, "Unknown command: ", runFlags.command) return 99 } } func main() { if len(os.Args) > 1 && (os.Args[1] == "-v" || os.Args[1] == "version") { fmt.Fprintf(os.Stdout, "ServCVgen version:%s\n",VERSION) os.Exit(0) } // if len(os.Args) > 1 || len(os.Getenv("CVGEN_COMMAND")) > 0 { // os.Exit(run()) // } else { // fmt.Fprintln(os.Stderr, "Use --help to see options") // } os.Exit(run()) }