コード例 #1
0
func (c *DecodeCommand) Run(args []string) int {
	var url string

	// Define option flag parse
	flags := flag.NewFlagSet(c.Name, flag.ContinueOnError)
	flags.SetOutput(c.ErrorWriter)
	flags.Usage = func() {}
	// Parse commandline flag
	if err := flags.Parse(args); err != nil {
		return ExitCodeError
	}
	// Parse argument
	switch flags.NArg() {
	case 0:
		url = ""
	case 1:
		url = flags.Arg(0)
	default:
		c.Ui.Error(os.ErrInvalid.Error() + ": " + flags.Arg(1))
		return ExitCodeError
	}

	if len(url) == 0 {
		// Get URL from STDIN
		scanner := bufio.NewScanner(c.Reader)
		if scanner.Scan() {
			url = strings.TrimSpace(scanner.Text())
		}
		if err := scanner.Err(); err != nil {
			c.Ui.Error(os.ErrInvalid.Error() + ": " + err.Error())
			return ExitCodeError
		}
	}

	if len(url) == 0 {
		c.Ui.Error(os.ErrInvalid.Error() + ": No Argument")
		return ExitCodeError
	}

	// shortening URL
	decodeUrl, err := gitioapi.Decode(&gitioapi.Param{Url: url})
	if err != nil {
		c.Ui.Error(err.Error() + "\n")
		return ExitCodeError
	}
	c.Ui.Output(decodeUrl)

	return ExitCodeOK
}
コード例 #2
0
func ExampleDecode() {
	shortUrl, _ := gitioapi.Decode(&gitioapi.Param{Url: "http://git.io/t"})
	fmt.Print(shortUrl)
	// https://github.com/technoweenie
}