Example #1
0
// copypaste from oldmain
func list(projectID string, accessToken string, include stories.IncludeOptions, style printing.PrintStyle) {

	_, h, err := terminal.GetSize(0)
	usePager := (err == nil)
	if _, err := exec.LookPath("less"); err != nil {
		usePager = false
	}

	// fetch stories
	numberOfStories := 0
	resp, fetcherror := stories.Fetch(projectID, accessToken, include)

	if fetcherror == nil {
		storyList := stories.Process(resp)
		numberOfStories = len(storyList)
		statusMap := stories.CountStories(storyList)

		usePager = usePager && (numberOfStories+3) > (h-1)

		if usePager {
			//http://stackoverflow.com/questions/21738674/how-do-i-print-a-buffer-to-stdout-piped-through-a-pager
			cmd := exec.Command("less")
			// create a pipe (blocking)
			r, stdin := io.Pipe()
			// Set your i/o's
			cmd.Stdin = r
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr

			c := make(chan struct{})
			go func() {
				defer close(c)
				cmd.Run()
			}()
			printing.PrintStories(stdin, storyList, style)
			printing.PrintStatusLine(stdin, style, statusMap)
			stdin.Close()
			<-c
		} else {
			printing.PrintStories(os.Stdout, storyList, style)
			printing.PrintStatusLine(os.Stdout, style, statusMap)
		}

	} else {
		fmt.Println(fetcherror)
		panic("fetch error?")
	}
}
Example #2
0
func main() {

	if len(os.Args) < 2 {
		fmt.Println("need to specify an action")
		os.Exit(1)
	}
	actionCommand := os.Args[1]
	action, actionErr := cli.ResolveAction(actionCommand)
	if actionErr != nil {
		panic(actionErr)
	}

	if action == cli.Init {
		coolPrompt()
		config.SetUp()
		os.Exit(0)
	}

	// panics if no config
	configuration := config.Configure()

	if action == cli.Switch {
		client := &http.Client{}

		req, reqErr := config.MeReqToken(configuration.Token)

		if reqErr != nil {
			panic(reqErr)
		}
		res, resErr := client.Do(req)

		if resErr != nil {
			panic(resErr)
		}
		defer res.Body.Close()
		resBytes, resErr := ioutil.ReadAll(res.Body)
		if resErr != nil {
			panic(resErr)
		}

		if res.StatusCode != 200 {
			fmt.Println(res.StatusCode)
			fmt.Println(string(resBytes))
			panic("bad response")
		}

		var me config.Me
		meResErrMarshErr := json.Unmarshal(resBytes, &me)
		if meResErrMarshErr != nil {
			panic(meResErrMarshErr)
		}
		config.Switch(me)
		os.Exit(0)
	}

	// only use these flags where appropriate, i.e. for
	// `list`, and `dash`, skip for `show`, `open`, start/finish/deliver
	isReport := cli.ActionIsReport(action)
	if isReport {

		// set up possible flags
		flags := flag.NewFlagSet(actionCommand, flag.ExitOnError)

		basic := flags.Bool("basic", false, "print in basic list")
		markdown := flags.Bool("markdown", false, "print in markdown list")
		urls := flags.Bool("urls", false, "just print urls")
		noColor := flags.Bool("no-color", false, "print without colorization")

		includeStarted := longShortBoolFlag(*flags, "started", "s", false, "include started")
		includeDelivered := longShortBoolFlag(*flags, "delivered", "d", false, "include delivered")
		includeFinished := longShortBoolFlag(*flags, "finished", "f", false, "include finished")
		includeUnstarted := longShortBoolFlag(*flags, "unstarted", "u", false, "include unstarted")
		includeRejected := longShortBoolFlag(*flags, "rejected", "r", false, "include rejected")

		label := longShortStringFlag(*flags, "labeled", "l", "", "include only stories with a given label")
		owner := longShortStringFlag(*flags, "owner", "o", "", "include only stories owned by the user with the given initials")

		// parse flags (loads cli options into vars above
		flags.Parse(os.Args[2:])

		// fmt.Printf("include: %v\n", inclusionMask)
		var style printing.PrintStyle
		if *basic {
			style = printing.Basic
		} else if *noColor {
			style = printing.TabularNoColor
		} else if *markdown {
			style = printing.Markdown
		} else if *urls {
			style = printing.URLs
		} else {
			style = printing.TabularColorized
		}
		if action == cli.List {
			listOpts := stories.IncludeOptions{
				*includeDelivered, *includeFinished, *includeStarted, *includeUnstarted, *includeRejected, *label, *owner,
			}
			list(configuration.Project, configuration.Token, listOpts, style)
		} else if action == cli.Dash {
			dashOpts := stories.IncludeOptions{
				true, true, true, true, true, *label, *owner,
			}
			list(configuration.Project, configuration.Token, dashOpts, style)
		} else if action == cli.Mine {
			dashOpts := stories.IncludeOptions{
				*includeDelivered, *includeFinished, *includeStarted, *includeUnstarted, *includeRejected, *label, configuration.UserInitials,
			}
			list(configuration.Project, configuration.Token, dashOpts, style)
		}

	} else {
		if action == cli.Open {
			storyIds := os.Args[2:]
			if len(storyIds) > 0 {
				open(storyIds)
			} else {
				openProject(configuration.Project)
			}
		} else if action == cli.Start {
			args := os.Args[2:]
			if len(args) > 0 {
				for _, arg := range args {
					fmt.Println("starting story", arg)
					startStory(arg, configuration)
				}
			} else {
				panic("need a single story id to start")
			}
		} else if action == cli.Estimate {
			args := os.Args[2:]
			if len(args) == 2 {
				est, estParseErr := strconv.Atoi(args[1])
				if estParseErr != nil {
					panic("estimate must be a number")
				}
				estimateStory(args[0], est, configuration)
			} else {
				panic("need a single story id and a point value to estimate")
			}
		} else if action == cli.Finish {
			args := os.Args[2:]
			if len(args) > 0 {
				for _, arg := range args {
					fmt.Println("finishing story", arg)
					finishStory(arg, configuration)
				}
			} else {
				panic("need a single story id to finish")
			}
		} else if action == cli.Deliver {
			args := os.Args[2:]
			if len(args) > 0 {
				for _, arg := range args {
					fmt.Println("delivering story", arg)
					deliverStory(arg, configuration)
				}
			} else {
				panic("need a single story id to deliver")
			}
		} else if action == cli.Accept {
			args := os.Args[2:]
			if len(args) > 0 {
				for _, arg := range args {
					fmt.Println("accepting story", arg)
					acceptStory(arg, configuration)
				}
			} else {
				panic("need a single story id to accept")
			}
		} else if action == cli.DeliverAll {
			finishedOnlyOpts := stories.IncludeOptions{
				false, true, false, false, false, "", "",
			}

			resp, fetcherror := stories.Fetch(configuration.Project, configuration.Token, finishedOnlyOpts)

			if fetcherror == nil {
				storyList := stories.Process(resp)
				for _, finishedStory := range storyList {
					idStr := strconv.FormatInt(finishedStory.ID, 10)
					fmt.Println("delivering story", idStr)
					deliverStory(idStr, configuration)
				}
			} else {
				fmt.Println("couldnt fetch stories")
				dumpResponse(&resp)
				panic(fetcherror)
			}
		} else {
			fmt.Printf("dont know what to do with %v for %v\n", actionCommand, os.Args[2:])
		}
	}

}