command

package
v0.0.0-...-ee38f14 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Success = gloss.NewStyle().Foreground(gloss.Color("10")).Background(gloss.Color("0"))
	Failure = gloss.NewStyle().Foreground(gloss.Color("9")).Background(gloss.Color("0"))
)
View Source
var New = &cobra.Command{
	Use:   "new",
	Short: "Create a new thing",
	Long:  "Create a new project or request",
	Run: func(cmd *cobra.Command, args []string) {
		_ = cmd.Help()
	},
}
View Source
var NewProject = &cobra.Command{
	Use:   "project",
	Short: "Create a new project",
	Long:  "Create a new project in the current folder",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		name := args[0]

		dir, err := os.Getwd()
		if err != nil {
			panic(err)
		}
		project, err := data.NewProject(dir, name)
		if err != nil {
			panic(err)
		}

		err = project.Save()
		if err != nil {
			panic(err)
		}
	},
}
View Source
var NewRequest = &cobra.Command{
	Use:   "request",
	Short: "Create a new request",
	Long:  "Create a new request in interactive mode",
	Args:  cobra.ExactArgs(0),
	Run: func(cmd *cobra.Command, args []string) {
		project, err := utils.LoadProject()
		if err != nil {
			panic(err)
		}

		var name, url, body, rawHeaders string
		var bodyType data.BodyType
		var method data.HTTPMethod

		basicInfoForm := huh.NewForm(
			huh.NewGroup(
				huh.NewInput().Title("Name").Value(&name),
				huh.NewInput().Title("URL").Value(&url),
				huh.NewSelect[data.HTTPMethod]().Title("Method").Value(&method).
					Options(
						huh.NewOption("GET", data.HTTPMethodGet),
						huh.NewOption("POST", data.HTTPMethodPost),
						huh.NewOption("PUT", data.HTTPMethodPut),
						huh.NewOption("DELETE", data.HTTPMethodDelete),
						huh.NewOption("PATCH", data.HTTPMethodPatch),
					),
				huh.NewText().Title("Headers").Placeholder("Header=value").Value(&rawHeaders),
			).Title("Create new sonata HTTP request"),
		)

		if err = basicInfoForm.Run(); err != nil {
			panic(err)
		}

		if method == data.HTTPMethodGet {
			bodyType = data.BodyTypeNone
		} else {
			err = huh.NewSelect[data.BodyType]().Title("Body type").Value(&bodyType).
				Options(
					huh.NewOption("None", data.BodyTypeJSON),
					huh.NewOption("JSON", data.BodyTypeJSON),
					huh.NewOption("Form", data.BodyTypeForm),
					huh.NewOption("Text", data.BodyTypeText),
				).
				Run()
			if err != nil {
				panic(err)
			}
			if bodyType != data.BodyTypeNone {
				body, err = editor.ReadFromEditor(data.BodyTypeExtensions[bodyType])
				if err != nil {
					panic(err)
				}
			}
		}

		headers := parseHeaders(rawHeaders)

		request := project.NewRequest(
			name,
			"",
			method,
			url,
			bodyType,
			body,
			headers,
		)
		if err := request.Save(); err != nil {
			panic(err)
		}
	},
}
View Source
var Run = &cobra.Command{
	Use:   "run",
	Short: "Run a request",
	Long:  "Run a request",
	Args:  cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		project, err := utils.LoadProject()
		if err != nil {
			panic(err)
		}

		name := strings.Join(args, " ")
		name = strings.TrimSuffix(name, ".request.json")

		request, found := project.GetRequest(name)
		if !found {
			panic("Request not found")
		}

		for _, env := range envs {
			err := godotenv.Load(env)
			if err != nil {
				panic(err)
			}
		}

		variables := parseRunVariables(runKeyValuePairs)

		client := client.NewClient()
		res, err := client.Run(request, variables)
		if err != nil {
			panic(err)
		}

		showRes(request, res)
	},
}
View Source
var (
	Version = &cobra.Command{
		Use:   "version",
		Short: "Print the version",
		Long:  "Print the current version",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf(
				"Sonata v%s (%s, compiled with %s for %s/%s)\n",
				data.CurrentVersion,
				runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH,
			)
		},
	}
)

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