Skip to content

Commit d01db9d

Browse files
[CLI]:Add download file (#426)
* Add download file * Add error handler * Refactor * Refactor * Make codegen and make lint * Refactor * FileName -> Filename * Fix * Bump * Bump * Bump * Bmp
1 parent 3bff2d0 commit d01db9d

File tree

7 files changed

+113
-4
lines changed

7 files changed

+113
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.385
1+
VERSION=v0.0.386
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/runtime.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24+
"mime"
2425
"net"
26+
"net/http"
2527
"net/url"
2628
"os"
29+
"regexp"
2730
"strconv"
2831
"strings"
2932
"sync"
@@ -176,6 +179,7 @@ func NewRuntimeCommand() *cobra.Command {
176179
cmd.AddCommand(NewRuntimeListCommand())
177180
cmd.AddCommand(NewRuntimeUninstallCommand())
178181
cmd.AddCommand(NewRuntimeUpgradeCommand())
182+
cmd.AddCommand(NewRuntimeLogsCommand())
179183

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

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

1957+
func NewRuntimeLogsCommand() *cobra.Command {
1958+
cmd := &cobra.Command{
1959+
Use: "logs [--ingress-host <url>] [--download]",
1960+
Short: "Work with current runtime logs",
1961+
RunE: func(cmd *cobra.Command, _ []string) error {
1962+
var err error = nil
1963+
if isAllRequiredFlagsForDownloadRuntimeLogs() {
1964+
err = downloadRuntimeLogs()
1965+
if err == nil {
1966+
log.G(cmd.Context()).Info("Runtime logs was downloaded successfully")
1967+
}
1968+
}
1969+
return err
1970+
},
1971+
}
1972+
cmd.Flags().BoolVar(&store.Get().IsDownloadRuntimeLogs, "download", false, "If true, will download logs from all componnents that consist of current runtime")
1973+
cmd.Flags().StringVar(&store.Get().IngressHost, "ingress-host", "", "Set runtime ingress host")
1974+
return cmd
1975+
}
1976+
1977+
func isAllRequiredFlagsForDownloadRuntimeLogs() bool {
1978+
return store.Get().IsDownloadRuntimeLogs && store.Get().IngressHost != ""
1979+
}
1980+
1981+
func downloadRuntimeLogs() error {
1982+
downloadFileUrl := getDownloadFileUrl()
1983+
response, err := http.Get(downloadFileUrl)
1984+
if err != nil {
1985+
return err
1986+
}
1987+
defer response.Body.Close()
1988+
fullFilename, err := getFullFilename(response)
1989+
if err != nil {
1990+
return err
1991+
}
1992+
return downloadFile(response, fullFilename)
1993+
}
1994+
1995+
func getDownloadFileUrl() string {
1996+
ingressHost := store.Get().IngressHost
1997+
appProxyPath := store.Get().AppProxyIngressPath
1998+
regularExpression := regexp.MustCompile(`([^:])/{2,}`)
1999+
url := fmt.Sprintf("%s/%s/api/applications/logs", ingressHost, appProxyPath)
2000+
return regularExpression.ReplaceAllString(url, `$1/`)
2001+
}
2002+
2003+
func getFullFilename(response *http.Response) (string, error) {
2004+
contentDisposition := response.Header.Get("Content-Disposition")
2005+
_, params, err := mime.ParseMediaType(contentDisposition)
2006+
if err != nil {
2007+
return "", err
2008+
}
2009+
filename := params["filename"]
2010+
processWorkingDirectory, err := os.Getwd()
2011+
if err != nil {
2012+
return "", err
2013+
}
2014+
fullFilename := fmt.Sprintf("%s/%s", processWorkingDirectory, filename)
2015+
return fullFilename, err
2016+
}
2017+
2018+
func downloadFile(response *http.Response, fullFilename string) error {
2019+
fileDescriptor, err := os.Create(fullFilename)
2020+
if err != nil {
2021+
return err
2022+
}
2023+
defer fileDescriptor.Close()
2024+
_, err = io.Copy(fileDescriptor, response.Body)
2025+
return err
2026+
}
2027+
19532028
func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtime.Runtime, rtConf *runtime.CommonConfig) error {
19542029
r, fs, err := cloneOpts.GetRepo(ctx)
19552030
if err != nil {

docs/commands/cli-v2_runtime.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cli-v2 runtime [flags]
2828
* [cli-v2](cli-v2.md) - cli-v2 is used for installing and managing codefresh installations using gitops
2929
* [cli-v2 runtime install](cli-v2_runtime_install.md) - Install a new Codefresh runtime
3030
* [cli-v2 runtime list](cli-v2_runtime_list.md) - List all Codefresh runtimes
31+
* [cli-v2 runtime logs](cli-v2_runtime_logs.md) - Work with current runtime logs
3132
* [cli-v2 runtime uninstall](cli-v2_runtime_uninstall.md) - Uninstall a Codefresh runtime
3233
* [cli-v2 runtime upgrade](cli-v2_runtime_upgrade.md) - Upgrade a Codefresh runtime
3334

docs/commands/cli-v2_runtime_logs.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## cli-v2 runtime logs
2+
3+
Work with current runtime logs
4+
5+
```
6+
cli-v2 runtime logs [--ingress-host <url>] [--download] [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
--download If true, will download logs from all componnents that consist of current runtime
13+
-h, --help help for logs
14+
--ingress-host string Set runtime ingress host
15+
```
16+
17+
### Options inherited from parent commands
18+
19+
```
20+
--auth-context string Run the next command using a specific authentication context
21+
--cfconfig string Custom path for authentication contexts config file (default "/home/user")
22+
--insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io)
23+
--insecure-ingress-host Disable certificate validation of ingress host (default: false)
24+
--request-timeout duration Request timeout (default 30s)
25+
--silent Disables the command wizard
26+
```
27+
28+
### SEE ALSO
29+
30+
* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes
31+

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

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

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

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

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.385
8+
version: 0.0.386
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/store/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ type Store struct {
144144
InstallationFlow string
145145
GsCreateFlow string
146146
InCluster string
147+
IsDownloadRuntimeLogs bool
148+
IngressHost string
147149
IscRuntimesDir string
148150
}
149151

0 commit comments

Comments
 (0)