client

package
v0.0.0-...-3963976 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Delete = &cli.Command{
	Name:      "delete",
	Usage:     "Delete a snippet",
	ArgsUsage: "<id>",
	Action: func(c *cli.Context) error {
		conf := LoadConfig()
		path := conf.Base + "/snippets/" + c.Args().First()

		req, err := http.NewRequest("DELETE", path, nil)
		if err != nil {
			return err
		}

		req.Header.Set("Authorization", conf.Auth)
		req.Header.Set("Content-Type", "application/json")

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			return err
		}

		if resp.StatusCode != 200 {
			return fmt.Errorf("failed to delete snippet: %s", resp.Status)
		}

		fmt.Println("Deleted snippet successfully.")

		return nil
	},
}
View Source
var Download = &cli.Command{
	Name:      "download",
	Usage:     "Download a snippet",
	ArgsUsage: "<id>",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "copy",
			Aliases: []string{"c"},
			Usage:   "Copy the snippet to the clipboard",
		},
	},
	Action: func(c *cli.Context) error {
		conf := LoadConfig()
		path := conf.Base + "/snippets/" + c.Args().First() + "/raw"

		resp, err := http.Get(path)
		if err != nil {
			return err
		}

		if resp.StatusCode != 200 {
			return fmt.Errorf("failed to download snippet: %s", resp.Status)
		}

		var content bytes.Buffer
		_, err = content.ReadFrom(resp.Body)
		if err != nil {
			return err
		}

		if !c.Bool("copy") {
			fmt.Println(content.String())
			return nil
		}

		err = clipboard.WriteAll(content.String())
		if err != nil {
			return err
		}

		fmt.Println("Copied snippet to clipboard.")

		return nil
	},
}
View Source
var Init = &cli.Command{
	Name:      "init",
	Usage:     "Initialise the client",
	ArgsUsage: "<base_url> <auth>",
	Action: func(c *cli.Context) error {
		baseUrl := c.Args().First()
		auth := c.Args().Get(1)

		conf := config{
			Base: baseUrl,
			Auth: auth,
		}
		SaveConfig(&conf)

		return nil
	},
}
View Source
var Upload = &cli.Command{
	Name:      "upload",
	Usage:     "Upload a snippet",
	ArgsUsage: "<id> <file>",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "language",
			Aliases: []string{"l"},
			Usage:   "Language of the snippet",
		},
	},
	Action: func(c *cli.Context) error {
		conf := LoadConfig()
		path := conf.Base + "/snippets/" + c.Args().First()

		data, err := os.ReadFile(c.Args().Get(1))
		if err != nil {
			return err
		}
		lang, _ := enry.GetLanguageByContent(c.Args().Get(1), data)

		if c.String("language") != "" {
			lang = c.String("language")
		}

		jsonData, err := json.Marshal(uploadSnippet{
			Language: lang,
			Content:  string(data),
		})
		if err != nil {
			return err
		}

		req, err := http.NewRequest("POST", path, bytes.NewReader(jsonData))
		if err != nil {
			return err
		}

		req.Header.Set("Authorization", conf.Auth)
		req.Header.Set("Content-Type", "application/json")

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			return err
		}

		if resp.StatusCode != 200 {
			return fmt.Errorf("failed to upload snippet: %s", resp.Status)
		}

		fmt.Println("Uploaded snippet successfully to " + path)

		return nil
	},
}

Functions

func LoadConfig

func LoadConfig() *config

func SaveConfig

func SaveConfig(conf *config)

Types

This section is empty.

Jump to

Keyboard shortcuts

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