예제 #1
0
파일: api.go 프로젝트: jonasi/project
	hateoas.Path("/commands"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		commander := getCommander(c)
		return commander.History(), nil
	})),
	hateoas.POST(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		var (
			logger    = plugin.GetLogger(c)
			commander = getCommander(c)
		)

		var args struct {
			Args []string `json:"args"`
		}

		if err := json.NewDecoder(mohttp.GetRequest(c).Body).Decode(&args); err != nil {
			logger.Error("JSON decoding error", "error", err)
			return nil, err
		}

		if len(args.Args) < 1 {
			return nil, fmt.Errorf("Invalid args")
		}

		return commander.Run(args.Args...)
	})),
)

var command = hateoas.NewResource(
	hateoas.Path("/commands/:id"),
	hateoas.AddLink("stdout", stdout),
예제 #2
0
파일: api.go 프로젝트: jonasi/project
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		name := mohttp.GetPathValues(c).Params.String("formula")
		f, err := getBrew(c).Info(name)

		if err != nil {
			return nil, err
		}

		if len(f.Installed) == 0 {
			return nil, mohttp.HTTPError(404)
		}

		return f, nil
	})),
	hateoas.POST(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		name := mohttp.GetPathValues(c).Params.String("formula")
		return getBrew(c).Install(name)
	})),
	hateoas.DELETE(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		name := mohttp.GetPathValues(c).Params.String("formula")
		return getBrew(c).Uninstall(name)
	})),
)

var search = hateoas.NewResource(
	hateoas.Path("/search"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		q := mohttp.GetPathValues(c).Query.String("q")
		return getBrew(c).Search(q)
	})),
)