Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .openapi-generator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
packageName: workflowclient
withGoMod: false
structPrefix: true
typeMappings:
integer: int

generateInterfaces: false
hideGenerationTimestamp: true

globalProperties:
apiTests: false
modelTests: false
apiDocs: false
modelDocs: false
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,6 @@ help:
@echo " install go install into \$$GOBIN"
@echo " test, vet, tidy standard Go targets"
@echo " clean remove ./bin and ./dist"

openapi-generator:
openapi-generator-cli generate -i openapi-public.yaml -g go -o ./internal/workflow_client -c .openapi-generator.yaml
29 changes: 19 additions & 10 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"time"

workflowclient "github.com/cloud-ru/evolution-devservices-cli/internal/workflow_client"
"github.com/spf13/cobra"

"github.com/cloud-ru/evolution-devservices-cli/internal/config"
Expand All @@ -23,11 +24,13 @@ const workflowTokenExpiryBuffer = 30 * time.Second
// runtimeContext holds the resolved configuration, API clients and printer
// for a single command invocation.
type runtimeContext struct {
Cfg *config.Config
API *repoapi.Client
WorkflowAPI *workflowapi.Client
Printer *output.Printer
Quiet bool
Cfg *config.Config
API *repoapi.Client
WorkflowAPI *workflowapi.Client
Printer *output.Printer
Quiet bool
ProjectID string
WorkflowClient *workflowclient.APIClient
}

// resolveContext loads config, applies flag/env overrides and constructs
Expand Down Expand Up @@ -72,12 +75,17 @@ func resolveContext(cmd *cobra.Command) (*runtimeContext, error) {
format = output.FormatJSON
}

workflowClientCfg := workflowclient.NewConfiguration()
workflowClientCfg.AddDefaultHeader("Authorization", "Bearer "+cfg.WorkflowAccessToken)

rt := &runtimeContext{
Cfg: cfg,
API: repoapi.New(cfg.APIURL, cfg.APIKey, cfg.ProjectID),
WorkflowAPI: workflowapi.New(cfg.WorkflowAPIURL, cfg.WorkflowAccessToken, cfg.ProjectID),
Printer: output.New(format),
Quiet: quiet,
Cfg: cfg,
API: repoapi.New(cfg.APIURL, cfg.APIKey, cfg.ProjectID),
WorkflowAPI: workflowapi.New(cfg.WorkflowAPIURL, cfg.WorkflowAccessToken, cfg.ProjectID),
Printer: output.New(format),
Quiet: quiet,
ProjectID: cfg.ProjectID,
WorkflowClient: workflowclient.NewAPIClient(workflowClientCfg),
}

// TEMPORARY: some prod environments don't yet accept X-API-KEY for the
Expand Down Expand Up @@ -144,6 +152,7 @@ func (r *runtimeContext) ensureWorkflowAuth(ctx context.Context) error {
r.Cfg.WorkflowTokenExpiresAt = expiresAt
r.Cfg.WorkflowAccessTokenKeyID = r.Cfg.WorkflowKeyID
r.WorkflowAPI.SetToken(tok.AccessToken)
r.WorkflowClient.GetConfig().DefaultHeader["Authorization"] = "Bearer " + tok.AccessToken

// Cache the token on disk (best-effort: a caching failure shouldn't
// fail the command, since we already have a valid token in memory).
Expand Down
44 changes: 24 additions & 20 deletions cmd/job.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cmd

import (
"bufio"
"fmt"
"strings"
"time"

workflowclient "github.com/cloud-ru/evolution-devservices-cli/internal/workflow_client"
"github.com/spf13/cobra"

"github.com/cloud-ru/evolution-devservices-cli/internal/output"
Expand Down Expand Up @@ -115,14 +115,15 @@ func newJobListCmd() *cobra.Command {
}

func newJobLogsCmd() *cobra.Command {
var (
limit int
beforeTime string
)

cmd := &cobra.Command{
Use: "logs <job-id>",
Short: "Stream logs for a job",
Short: "Get logs for a job",
Args: cobra.ExactArgs(1),
Long: `logs opens the job's log stream (server-sent events) and prints
each log line to stdout as it arrives. For a finished job the stream ends
as soon as the buffered log has been sent; for a running job it stays open
until the job finishes or the command is interrupted (Ctrl-C).`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := resolveContext(cmd)
if err != nil {
Expand All @@ -132,25 +133,28 @@ until the job finishes or the command is interrupted (Ctrl-C).`,
return err
}

body, err := ctx.WorkflowAPI.StreamJobLogs(cmd.Context(), args[0])
resp, _, err := ctx.WorkflowClient.WorkflowsAPI.
ProjectProjectIdJobJobIdLogListPost(cmd.Context(), ctx.ProjectID, args[0]).
Request(workflowclient.GitSbercloudTechDsworksServicesPipelineSrcInternalApplicationRequestJobLogList{
BeforeTime: &beforeTime,
Limit: &limit,
}).Execute()
if err != nil {
return err
return fmt.Errorf("workflow client logs: %w", err)
}
defer body.Close()

scanner := bufio.NewScanner(body)
scanner.Buffer(make([]byte, 64*1024), 1024*1024)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "data:") {
// Skip SSE "event:" lines, ":" heartbeat comments and blank separators.
continue
}
fmt.Fprintln(cmd.OutOrStdout(), strings.TrimSpace(strings.TrimPrefix(line, "data:")))
err = ctx.Printer.PrintJSON(resp)
if err != nil {
return fmt.Errorf("print json: %w", err)
}
return scanner.Err()

return nil
},
}

cmd.Flags().IntVar(&limit, "limit", 10, "page size, default is 10")
cmd.Flags().StringVar(&beforeTime, "before", time.Now().Format(time.RFC3339), "RFC3339 time, default is now")

return cmd
}

Expand Down
24 changes: 24 additions & 0 deletions internal/workflow_client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
23 changes: 23 additions & 0 deletions internal/workflow_client/.openapi-generator-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
42 changes: 42 additions & 0 deletions internal/workflow_client/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.gitignore
.openapi-generator-ignore
.travis.yml
README.md
api/openapi.yaml
api_pipelines.go
api_services.go
api_workflows.go
client.go
configuration.go
git_push.sh
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_application.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_config_storage_type.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_deployment.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_job.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_job_status.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_pipeline.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_pipeline_type.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_repo_config_storage.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_run.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_run_status.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_model_stage.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_request_application_create.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_request_application_update.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_request_job_log_list.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_application_create.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_application_get.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_application_list.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_deployment_create.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_deployment_list.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_deployment_response.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_job_log_list.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_job_logs.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_pipeline_run.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_application_response_run_get.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_errors_bad_request.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_errors_conflict.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_errors_internal.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_errors_not_found.go
model_git_sbercloud_tech_dsworks_services_pipeline_src_internal_errors_unauthorized.go
response.go
utils.go
1 change: 1 addition & 0 deletions internal/workflow_client/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.24.0
8 changes: 8 additions & 0 deletions internal/workflow_client/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: go

install:
- go get -d -v .

script:
- go build -v ./

Loading
Loading