func ClearConfig() { apiKey := config.Load().Api_key workspace := config.Load().Workspace f, _ := os.Create(utils.Home() + "/.asana.yml") f.WriteString("api_key: " + apiKey + "\n") f.WriteString("workspace: " + strconv.Itoa(workspace) + "\n") }
func Tasks(params url.Values, withCompleted bool, withProject bool) []Task_t { params.Add("opt_fields", "name,completed,due_on,assignee.name,projects.name,tags.name") var tasks Tasks_t if withProject { uri := "/api/1.0/projects/" + strconv.Itoa(config.Load().Project) + "/tasks" err := json.Unmarshal(Get(uri, params), &tasks) utils.Check(err) } else { params.Add("workspace", strconv.Itoa(config.Load().Workspace)) params.Add("assignee", "me") params.Add("limit", "100") err := json.Unmarshal(Get("/api/1.0/tasks", params), &tasks) utils.Check(err) } var tasks_with_due []Task_t for _, t := range tasks.Data { if !withCompleted && t.Completed { continue } if strings.HasSuffix(t.Name, ":") { continue } if HasTagsBlacklisted(t) { continue } tasks_with_due = append(tasks_with_due, t) } return tasks_with_due }
func Config(c *cli.Context) { _, err := ioutil.ReadFile(utils.Home() + "/.asana.yml") if err != nil || config.Load().Api_key == "" { println("visit: http://app.asana.com/-/account_api") print("paste your api_key: ") var s string fmt.Scanf("%s", &s) f, _ := os.Create(utils.Home() + "/.asana.yml") defer f.Close() f.WriteString("api_key: " + s + "\n") } ws := api.Me().Workspaces index := 0 cyan := ansi.ColorCode("cyan") reset := ansi.ColorCode("reset") if len(ws) > 1 { fmt.Println("\n" + strconv.Itoa(len(ws)) + " workspaces found.") for i, w := range ws { fmt.Printf("%s[%d]%s %s\n", cyan, i, reset, w.Name) } index = utils.EndlessSelect(len(ws)-1, index) } apiKey := config.Load().Api_key f, _ := os.Create(utils.Home() + "/.asana.yml") f.WriteString("api_key: " + apiKey + "\n") f.WriteString("workspace: " + strconv.Itoa(ws[index].Id) + "\n") }
func CreateTask(withProject bool, name string) Task_t { workspace := strconv.Itoa(config.Load().Workspace) data := `{ "data" : { "assignee": "me", "workspace": ` + workspace + `, "name": "` + name + `" } }` respBody := Post("/tasks", data) var output map[string]Task_t err := json.Unmarshal(respBody, &output) utils.Check(err) newTask := output["data"] if withProject { p := strconv.Itoa(config.Load().Project) t := strconv.Itoa(newTask.Id) uri := "/tasks/" + t + "/addProject" dd := `{ "data": { "project": ` + p + ` } }` Post(uri, dd) } return newTask }
func FindUserId(name string) string { var users map[string][]User_t uri := "/api/1.0/workspaces/" + strconv.Itoa(config.Load().Workspace) + "/typeahead?type=user&query=" + name err := json.Unmarshal(Get(uri, nil), &users) utils.Check(err) return strconv.Itoa(users["data"][0].Id) }
func Projects() []Project_t { var projects map[string][]Project_t uri := "/api/1.0/workspaces/" + strconv.Itoa(config.Load().Workspace) + "/projects" err := json.Unmarshal(Get(uri, nil), &projects) utils.Check(err) return projects["data"] }
func Tags() Tags_t { var tags Tags_t uri := "/api/1.0/workspaces/" + strconv.Itoa(config.Load().Workspace) + "/tags" err := json.Unmarshal(Get(uri, nil), &tags) utils.Check(err) return tags }
func Projects(c *cli.Context) { projects := api.Projects() index := 0 cyan := ansi.ColorCode("cyan") reset := ansi.ColorCode("reset") if len(projects) > 0 { fmt.Println("\n" + strconv.Itoa(len(projects)) + " projects found.") for i, p := range projects { fmt.Printf("%s[%2d]%s %s\n", cyan, i, reset, p.Name) } index = utils.EndlessSelect(len(projects)-1, index) } workspace := config.Load().Workspace apiKey := config.Load().Api_key proj := strconv.Itoa(projects[index].Id) f, _ := os.Create(utils.Home() + "/.asana.yml") f.WriteString("api_key: " + apiKey + "\n") f.WriteString("workspace: " + strconv.Itoa(workspace) + "\n") f.WriteString("project: " + proj + "\n") }
func CreateTag(tagname string) Tag_t { workspace := strconv.Itoa(config.Load().Workspace) data := `{ "data": { "workspace": ` + workspace + `, "name": "` + tagname + `" } }` respBody := Post("/tags", data) var output map[string]Tag_t err := json.Unmarshal(respBody, &output) utils.Check(err) newTag := output["data"] return newTag }
func fire(req *http.Request) []byte { client := &http.Client{} req.Header.Set("User-Agent", UserAgent) req.SetBasicAuth(config.Load().Api_key, "") resp, err := client.Do(req) utils.Check(err) body, err := ioutil.ReadAll(resp.Body) utils.Check(err) if resp.StatusCode >= 300 { println(resp.Status) } return body }
func isWithProject() bool { p := config.Load().Project return p != 0 }