示例#1
0
func main() {
	ll := flag.String("log", "ERROR", "Set the log level")
	version := flag.Bool("version", false, "Display the version number")

	configDir, err := getConfigPath()
	if err != nil {
		panic(err)
	}

	envFile := configDir + "/ssh-manage.env"
	_, err = os.Stat(envFile)
	if err == nil {
		loadConfig(envFile)
	}

	d := diskv.New(diskv.Options{
		BasePath:     configDir + "/hosts", // where the data is stored
		Transform:    BlockTransform,
		CacheSizeMax: 1024 * 1024, // 1MB
	})

	flag.Usage = usage
	flag.Parse()

	if *version {
		fmt.Printf("%s version: %v\n", os.Args[0], VERSION)
		os.Exit(0)
	}

	logLevel := getLogLevel(*ll)
	l = llog.New(os.Stdout, logLevel)

	logHandler("DEBUG", fmt.Sprintln("configuration directory:", configDir))

	if flag.NArg() == 0 {
		logHandler("ERROR", "please supply a command")
		// TODO list supported commands (Redirect to help message or usage text?)
		os.Exit(1)
	}

	// TODO add the ability to set if a record or records should get
	// printed.  This needs to be host dependant.
	switch flag.Arg(0) {
	case "add":
		var hostInfo string
		if flag.Arg(2) != "" {
			hostInfo = flag.Arg(2)
		}

		err = addRecord(d, strings.TrimSpace(flag.Arg(1)), hostInfo)
		if err != nil {
			logHandler("ERROR",
				fmt.Sprintf("failed creating a new record: %s\n", err.Error()))
			os.Exit(1)
		}
	case "get":
		err := getRecord(d, strings.TrimSpace(flag.Arg(1)))
		if err != nil {
			logHandler("ERROR",
				fmt.Sprintf("failed fetching record details: %s\n", err.Error()))
			os.Exit(1)
		}
	case "list":
		err := listRecords(d)
		if err != nil {
			logHandler("ERROR",
				fmt.Sprintf("failed fetching all records: %s\n", err.Error()))
			os.Exit(1)
		}
	case "rm":
		err := removeRecord(d, strings.TrimSpace(flag.Arg(1)))
		if err != nil {
			logHandler("ERROR",
				fmt.Sprintf("failed removing record: %s\n", err.Error()))
			os.Exit(1)
		}
	case "write":
		err := writeFile(d)
		if err != nil {
			logHandler("ERROR",
				fmt.Sprintf("failed when writing out SSH configuration file: %s\n",
					err.Error()))
			os.Exit(1)
		}
	case "update":
		if flag.Arg(1) == "" {
			logHandler("ERROR", "update requires an argument")
			os.Exit(1)
		}

		err := updateRecord(d, strings.TrimSpace(flag.Arg(1)))
		if err != nil {
			logHandler("ERROR",
				fmt.Sprintf("faild updating record: %s\n", err.Error()))
			os.Exit(1)
		}
	default:
		usage()
		os.Exit(1)
	}

	os.Exit(0)
}
示例#2
0
文件: main.go 项目: vendion/trashctl
func main() {
	flag.Usage = usage
	debug := flag.Bool("debug", false, "Show debug output")
	version := flag.Bool("version", false, "Display the version number")
	verbose := flag.Bool("verbose", false, "Display verbose output")
	flag.Parse()

	if *version {
		fmt.Printf("%s version: %v\n", os.Args[0], VERSION)
		os.Exit(0)
	}

	if *debug {
		level = "DEBUG"
		l = llog.New(os.Stdout, llog.DEBUG)
	} else if *verbose {
		level = "INFO"
		l = llog.New(os.Stdout, llog.INFO)
	} else {
		l = llog.New(os.Stdout, llog.WARNING)
	}

	if flag.NArg() == 0 {
		logHandler("ERROR", "a subcommand is needed")
		usage()
		os.Exit(2)
	}

	logHandler("DEBUG", fmt.Sprintf("subcommand given: %s", flag.Arg(0)))
	switch flag.Arg(0) {
	case "add":
		args := flag.Args()
		args = getFilesList(args)
		logHandler("DEBUG", fmt.Sprintf("%#v", args))
		err := addFile(args)
		if err != nil {
			logHandler("ERROR", err.Error())
			os.Exit(1)
		}
	case "list":
		err := listFiles()
		if err != nil {
			logHandler("ERROR", err.Error())
			os.Exit(1)
		}
	case "restore":
		args := flag.Args()
		args = getFilesList(args)
		logHandler("DEBUG", fmt.Sprintf("%#v", args))
		err := restoreFiles(args)
		if err != nil {
			logHandler("ERROR", err.Error())
			os.Exit(1)
		}
	case "rm":
		args := flag.Args()
		args = getFilesList(args)
		logHandler("DEBUG", fmt.Sprintf("%#v", args))
		err := deleteFile(args)
		if err != nil {
			logHandler("ERROR", err.Error())
			os.Exit(1)
		}
	case "empty":
		err := emptyTrash()
		if err != nil {
			logHandler("ERROR", err.Error())
			os.Exit(1)
		}
	default:
		logHandler("ERROR", fmt.Sprintf("unknown subcommand: %s", flag.Arg(0)))
		usage()
		os.Exit(2)
	}
	os.Exit(0)
}