Giter VIP home page Giter VIP logo

Comments (4)

lispyclouds avatar lispyclouds commented on May 24, 2024 1

Thanks a lot for the ideas! Looks good for me to go explore things, will get back if something more could be done! πŸ˜„

from restish.

danielgtaylor avatar danielgtaylor commented on May 24, 2024

It's possible to add your own custom formats, auth, commands, etc to Restish pretty easily, for example the main.go setup just looks like this:

cli.Init("restish", version)

// Register default encodings, content type handlers, and link parsers.
cli.Defaults()

// Register format loaders to auto-discover API descriptions
cli.AddLoader(openapi.New())

// Register auth schemes
cli.AddAuth("oauth-client-credentials", &oauth.ClientCredentialsHandler{})
cli.AddAuth("oauth-authorization-code", &oauth.AuthorizationCodeHandler{})

// Run the CLI, parsing arguments, making requests, and printing responses.
cli.Run()

You can do a cli.Root.AddCommand(&cobra.Command{......}) before calling cli.Run().

If you prefer to just get a bunch of pre-configured commands and inject those into your CLI, I haven't tried that but you might be able to. There is a cli.Load(entrypoint string, root *cobra.Command) you can try to use. You'll still need to cli.AddLoader(openapi.New()) if you want it to recognize OpenAPI.

Any improvements to make this easier or better are welcome! πŸ‘


Edit: I'm also contemplating whether it would be worth supporting some kind of plugin or scripting functionality to enable custom commands. πŸ€”

from restish.

lispyclouds avatar lispyclouds commented on May 24, 2024

Thanks a lot for the pointers, will try them out soon! πŸ˜„ As for the extensibility aspects, it would be great, at least for me, if I can just hook in my commands which aren't necessarily talking to the API. Also maybe a way to customise the handler of the OperationId, the way i was thinking before I found restish is taking a map like:

map[string]func(params map[string]any) error{
  "opid1": func(params map[string]any) error {
    // some potential side-effects
    return err
  },
  ...
}

and passing it along to the init or something? I think having something like this enables more custom behaviour while still keeping the underlying things same?

from restish.

danielgtaylor avatar danielgtaylor commented on May 24, 2024

As for the extensibility aspects, it would be great, at least for me, if I can just hook in my commands which aren't necessarily talking to the API.

Using cli.Root.AddCommand(&cobra.Command{......}) lets you add any custom command you want, for example:

cli.Init("my-cli", "1.0.0")

cli.Root.AddCommand(&cobra.Command{
	Use:  "hello",
	Args: cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		// You can do anything you want here, no need to talk to any API!
		fmt.Printf("Hello, %s!\n", args[0])
	},
})

cli.Run()

Then you can try it out:

$ go run . hello lispyclouds
Hello lispyclouds!

from restish.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.