Skip to content

[CLI]:Add download file #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 14, 2022
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.385
VERSION=v0.0.386

OUT_DIR=dist
YEAR?=$(shell date +"%Y")
Expand Down
75 changes: 75 additions & 0 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ import (
"errors"
"fmt"
"io"
"mime"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -176,6 +179,7 @@ func NewRuntimeCommand() *cobra.Command {
cmd.AddCommand(NewRuntimeListCommand())
cmd.AddCommand(NewRuntimeUninstallCommand())
cmd.AddCommand(NewRuntimeUpgradeCommand())
cmd.AddCommand(NewRuntimeLogsCommand())

cmd.PersistentFlags().BoolVar(&store.Get().Silent, "silent", false, "Disables the command wizard")

Expand Down Expand Up @@ -1950,6 +1954,77 @@ func RunRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error {
return nil
}

func NewRuntimeLogsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "logs [--ingress-host <url>] [--download]",
Short: "Work with current runtime logs",
RunE: func(cmd *cobra.Command, _ []string) error {
var err error = nil
if isAllRequiredFlagsForDownloadRuntimeLogs() {
err = downloadRuntimeLogs()
if err == nil {
log.G(cmd.Context()).Info("Runtime logs was downloaded successfully")
}
}
return err
},
}
cmd.Flags().BoolVar(&store.Get().IsDownloadRuntimeLogs, "download", false, "If true, will download logs from all componnents that consist of current runtime")
cmd.Flags().StringVar(&store.Get().IngressHost, "ingress-host", "", "Set runtime ingress host")
return cmd
}

func isAllRequiredFlagsForDownloadRuntimeLogs() bool {
return store.Get().IsDownloadRuntimeLogs && store.Get().IngressHost != ""
}

func downloadRuntimeLogs() error {
downloadFileUrl := getDownloadFileUrl()
response, err := http.Get(downloadFileUrl)
if err != nil {
return err
}
defer response.Body.Close()
fullFilename, err := getFullFilename(response)
if err != nil {
return err
}
return downloadFile(response, fullFilename)
}

func getDownloadFileUrl() string {
ingressHost := store.Get().IngressHost
appProxyPath := store.Get().AppProxyIngressPath
regularExpression := regexp.MustCompile(`([^:])/{2,}`)
url := fmt.Sprintf("%s/%s/api/applications/logs", ingressHost, appProxyPath)
return regularExpression.ReplaceAllString(url, `$1/`)
}

func getFullFilename(response *http.Response) (string, error) {
contentDisposition := response.Header.Get("Content-Disposition")
_, params, err := mime.ParseMediaType(contentDisposition)
if err != nil {
return "", err
}
filename := params["filename"]
processWorkingDirectory, err := os.Getwd()
if err != nil {
return "", err
}
fullFilename := fmt.Sprintf("%s/%s", processWorkingDirectory, filename)
return fullFilename, err
}

func downloadFile(response *http.Response, fullFilename string) error {
fileDescriptor, err := os.Create(fullFilename)
if err != nil {
return err
}
defer fileDescriptor.Close()
_, err = io.Copy(fileDescriptor, response.Body)
return err
}

func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtime.Runtime, rtConf *runtime.CommonConfig) error {
r, fs, err := cloneOpts.GetRepo(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/commands/cli-v2_runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cli-v2 runtime [flags]
* [cli-v2](cli-v2.md) - cli-v2 is used for installing and managing codefresh installations using gitops
* [cli-v2 runtime install](cli-v2_runtime_install.md) - Install a new Codefresh runtime
* [cli-v2 runtime list](cli-v2_runtime_list.md) - List all Codefresh runtimes
* [cli-v2 runtime logs](cli-v2_runtime_logs.md) - Work with current runtime logs
* [cli-v2 runtime uninstall](cli-v2_runtime_uninstall.md) - Uninstall a Codefresh runtime
* [cli-v2 runtime upgrade](cli-v2_runtime_upgrade.md) - Upgrade a Codefresh runtime

31 changes: 31 additions & 0 deletions docs/commands/cli-v2_runtime_logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## cli-v2 runtime logs

Work with current runtime logs

```
cli-v2 runtime logs [--ingress-host <url>] [--download] [flags]
```

### Options

```
--download If true, will download logs from all componnents that consist of current runtime
-h, --help help for logs
--ingress-host string Set runtime ingress host
```

### Options inherited from parent commands

```
--auth-context string Run the next command using a specific authentication context
--cfconfig string Custom path for authentication contexts config file (default "/home/user")
--insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io)
--insecure-ingress-host Disable certificate validation of ingress host (default: false)
--request-timeout duration Request timeout (default 30s)
--silent Disables the command wizard
```

### SEE ALSO

* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes

4 changes: 2 additions & 2 deletions docs/releases/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cf version

```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.385/cf-linux-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.386/cf-linux-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-linux-amd64 /usr/local/bin/cf
Expand All @@ -36,7 +36,7 @@ cf version

```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.385/cf-darwin-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.386/cf-darwin-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-darwin-amd64 /usr/local/bin/cf
Expand Down
2 changes: 1 addition & 1 deletion manifests/runtime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: "{{ namespace }}"
spec:
defVersion: 1.0.1
version: 0.0.385
version: 0.0.386
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
components:
- name: events
Expand Down
2 changes: 2 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ type Store struct {
InstallationFlow string
GsCreateFlow string
InCluster string
IsDownloadRuntimeLogs bool
IngressHost string
IscRuntimesDir string
}

Expand Down