cmd: add version command

This commit is contained in:
Carlo Mandelli 2019-10-04 11:32:07 +02:00
parent 43586549f3
commit 4fc8f3ebed
7 changed files with 161 additions and 1 deletions

View File

@ -125,7 +125,7 @@ func serve(cmd *cobra.Command, args []string) error {
}
for _, ec := range serveOpts.components {
if !util.StringInSlice(componentsNames, ec) {
return errors.Errorf("unkown component name %q", ec)
return errors.Errorf("unknown component name %q", ec)
}
}

52
cmd/agola/cmd/version.go Normal file
View File

@ -0,0 +1,52 @@
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"fmt"
gwclient "agola.io/agola/services/gateway/client"
"github.com/spf13/cobra"
)
var cmdVersion = &cobra.Command{
Use: "version",
Short: "version",
Run: func(cmd *cobra.Command, args []string) {
if err := printVersions(cmd, args); err != nil {
log.Fatalf("err: %v", err)
}
},
}
func init() {
cmdAgola.AddCommand(cmdVersion)
}
func printVersions(cmd *cobra.Command, args []string) error {
gwclient := gwclient.NewClient(gatewayURL, token)
gwversion, _, err := gwclient.GetVersion(context.TODO())
if err != nil {
return err
}
fmt.Printf("Gateway version:\t%s\n", gwversion.Version)
fmt.Printf("Client version: \t%s\n", cmdAgola.Version)
return nil
}

View File

@ -0,0 +1,32 @@
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package action
import (
"context"
"agola.io/agola/cmd"
gwapitypes "agola.io/agola/services/gateway/api/types"
)
func (h *ActionHandler) GetVersion(ctx context.Context) (*gwapitypes.VersionResponse, error) {
v := &gwapitypes.VersionResponse{
Service: "gateway",
Version: cmd.Version,
}
return v, nil
}

View File

@ -0,0 +1,46 @@
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"net/http"
"agola.io/agola/internal/services/gateway/action"
"go.uber.org/zap"
)
type VersionHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewVersionHandler(logger *zap.Logger, ah *action.ActionHandler) *VersionHandler {
return &VersionHandler{log: logger.Sugar(), ah: ah}
}
func (h *VersionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
version, err := h.ah.GetVersion(ctx)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
if err := httpResponse(w, http.StatusOK, version); err != nil {
h.log.Errorf("err: %+v", err)
}
}

View File

@ -215,6 +215,8 @@ func (g *Gateway) Run(ctx context.Context) error {
badgeHandler := api.NewBadgeHandler(logger, g.ah)
versionHandler := api.NewVersionHandler(logger, g.ah)
reposHandler := api.NewReposHandler(logger, g.c.GitserverURL)
loginUserHandler := api.NewLoginUserHandler(logger, g.ah)
@ -304,6 +306,8 @@ func (g *Gateway) Run(ctx context.Context) error {
apirouter.Handle("/badges/{projectref}", badgeHandler).Methods("GET")
apirouter.Handle("/version", versionHandler).Methods("GET")
// TODO(sgotti) add auth to these requests
reposRouter.Handle("/repos/{rest:.*}", reposHandler).Methods("GET", "POST")

View File

@ -0,0 +1,20 @@
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package types
type VersionResponse struct {
Service string `json:"service"`
Version string `json:"version"`
}

View File

@ -584,3 +584,9 @@ func (c *Client) GetOrgMembers(ctx context.Context, orgRef string) (*gwapitypes.
resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/orgs/%s/members", orgRef), nil, jsonContent, nil, &res)
return res, resp, err
}
func (c *Client) GetVersion(ctx context.Context) (*gwapitypes.VersionResponse, *http.Response, error) {
res := &gwapitypes.VersionResponse{}
resp, err := c.getParsedResponse(ctx, "GET", "/version", nil, jsonContent, nil, &res)
return res, resp, err
}