get

package
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2021 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Cmd = &cobra.Command{
		Use:       "get [RESOURCE]",
		Short:     "Display one resource",
		ValidArgs: []string{"tasks", "results", "goals"},
		Args:      cobra.MaximumNArgs(2),
		Run: func(cmd *cobra.Command, args []string) {
			page, _ := cmd.Flags().GetInt("page")
			size, _ := cmd.Flags().GetInt("size")

			if len(args) == 2 {
				id := args[1]

				switch args[0] {
				case "tasks":
					uri := fmt.Sprintf("%v/v1/tasks/%v", cfg.API.EndPoint, id)
					req, err := http.NewRequest(http.MethodGet, uri, nil)
					if err != nil {
						fmt.Println(err)
						return
					}

					client := &http.Client{}
					resp, err := client.Do(req)
					if err != nil {
						fmt.Println(err)
						return
					}
					defer func() {
						_ = resp.Body.Close()
					}()

					if resp.StatusCode == http.StatusNotFound {
						fmt.Println("No resource found")
						return
					}

					body, err := ioutil.ReadAll(resp.Body)
					if err != nil {
						fmt.Println(err)
						return
					}

					var task *todo.Task
					err = json.Unmarshal(body, &task)
					if err != nil {
						fmt.Println(err)
						return
					}

					table := tablewriter.NewWriter(os.Stdout)
					table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
					table.SetHeader([]string{"ID", "Title", "Status", "Create At"})
					table.Append(task.ToLine())
					table.Render()

					break
				case "goals":
					uri := fmt.Sprintf("%v/v1/goals/%v", cfg.API.EndPoint, id)
					req, err := http.NewRequest(http.MethodGet, uri, nil)
					if err != nil {
						fmt.Println(err)
						return
					}

					client := &http.Client{}
					resp, err := client.Do(req)
					if err != nil {
						fmt.Println(err)
						return
					}
					defer resp.Body.Close()

					body, err := ioutil.ReadAll(resp.Body)
					if err != nil {
						fmt.Println(err)
						return
					}

					var obj *okr.Objective
					err = json.Unmarshal(body, &obj)
					if err != nil {
						fmt.Println(err)
						return
					}

					table := tablewriter.NewWriter(os.Stdout)
					table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
					table.SetHeader([]string{"ID", "Title", "Start At", "End At", "Create At"})
					table.Append(obj.ToLine())
					table.Render()

					break
				case "results":
					uri := fmt.Sprintf("%v/v1/results/%v", cfg.API.EndPoint, id)
					req, err := http.NewRequest(http.MethodGet, uri, nil)
					if err != nil {
						fmt.Println(err)
						return
					}

					client := &http.Client{}
					resp, err := client.Do(req)
					if err != nil {
						fmt.Println(err)
						return
					}
					defer resp.Body.Close()

					body, err := ioutil.ReadAll(resp.Body)
					if err != nil {
						fmt.Println(err)
						return
					}

					var data *okr.KeyResult
					err = json.Unmarshal(body, &data)
					if err != nil {
						fmt.Println(err)
						return
					}

					table := tablewriter.NewWriter(os.Stdout)
					table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
					table.SetHeader([]string{"ID", "Goad ID", "Title", "Target", "Actual", "Create At"})
					table.Append(data.ToLine())
					table.Render()

					break
				}
			}

			if len(args) == 1 {
				switch args[0] {
				case "tasks":
					uri := fmt.Sprintf("%v/v1/tasks?page=%v&size=%v", cfg.API.EndPoint, page, size)
					req, err := http.NewRequest(http.MethodGet, uri, nil)
					if err != nil {
						fmt.Println(err)
						return
					}

					client := &http.Client{}
					resp, err := client.Do(req)
					if err != nil {
						fmt.Println(err)
						return
					}
					defer resp.Body.Close()

					if resp.StatusCode == http.StatusNotFound {
						fmt.Println("No resources found")
						return
					}

					body, err := ioutil.ReadAll(resp.Body)
					if err != nil {
						fmt.Println(err)
						return
					}

					var tasks []*todo.Task
					err = json.Unmarshal(body, &tasks)
					if err != nil {
						fmt.Println(err)
						return
					}

					table := tablewriter.NewWriter(os.Stdout)
					table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
					table.SetHeader([]string{"ID", "Title", "Status", "Create At"})
					for _, t := range tasks {
						table.Append(t.ToLine())
					}
					table.Render()

					break
				case "goals":
					uri := fmt.Sprintf("%v/v1/goals?page=%v&size=%v", cfg.API.EndPoint, page, size)
					req, err := http.NewRequest(http.MethodGet, uri, nil)
					if err != nil {
						fmt.Println(err)
						return
					}

					client := &http.Client{}
					resp, err := client.Do(req)
					if err != nil {
						fmt.Println(err)
						return
					}
					defer resp.Body.Close()

					if resp.StatusCode == http.StatusNotFound {
						fmt.Println("No resources found")
						return
					}

					body, err := ioutil.ReadAll(resp.Body)
					if err != nil {
						fmt.Println(body)
						return
					}

					var goals []*okr.Objective
					err = json.Unmarshal(body, &goals)
					if err != nil {
						fmt.Println(err)
						return
					}

					table := tablewriter.NewWriter(os.Stdout)
					table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
					table.SetHeader([]string{"ID", "Title", "Start At", "End At", "Create At"})
					for _, g := range goals {
						table.Append(g.ToLine())
					}
					table.Render()

					break
				case "results":
					uri := fmt.Sprintf("%v/v1/krs?page=%v&size=%v", cfg.API.EndPoint, page, size)
					req, err := http.NewRequest(http.MethodGet, uri, nil)
					if err != nil {
						fmt.Println(err)
						return
					}

					client := &http.Client{}
					resp, err := client.Do(req)
					if err != nil {
						fmt.Println(err)
						return
					}
					defer resp.Body.Close()

					if resp.StatusCode == http.StatusNotFound {
						fmt.Println("No resources found")
						return
					}

					body, err := ioutil.ReadAll(resp.Body)
					if err != nil {
						fmt.Println(err)
						return
					}

					var tasks []*okr.KeyResult
					err = json.Unmarshal(body, &tasks)
					if err != nil {
						fmt.Println(err)
						return
					}

					table := tablewriter.NewWriter(os.Stdout)
					table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
					table.SetHeader([]string{"ID", "Goad ID", "Title", "Target", "Actual", "Create At"})
					for _, t := range tasks {
						table.Append(t.ToLine())
					}
					table.Render()

					break
				}
			}
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL