Skip to content

Commit a31957f

Browse files
committed
feat: dav client added to new main
- dav/app used as proxy to be compatible with existing storager interface - dav/main deleted - config reader created for dav - defer close added to main for config file
1 parent 228aa14 commit a31957f

7 files changed

Lines changed: 154 additions & 110 deletions

File tree

‎dav/app/app.go‎

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,80 @@
11
package app
22

33
import (
4-
"encoding/json"
54
"errors"
6-
"flag"
75
"fmt"
8-
"io"
9-
"os"
6+
"time"
107

118
davcmd "github.com/cloudfoundry/storage-cli/dav/cmd"
129
davconfig "github.com/cloudfoundry/storage-cli/dav/config"
1310
)
1411

1512
type App struct {
1613
runner davcmd.Runner
14+
config davconfig.Config
1715
}
1816

19-
func New(runner davcmd.Runner) (app App) {
20-
app.runner = runner
21-
return
17+
func New(r davcmd.Runner, c davconfig.Config) *App {
18+
app := &App{runner: r, config: c}
19+
return app
2220
}
2321

24-
func (app App) Run(args []string) (err error) {
25-
args = args[1:]
26-
var configFilePath string
27-
var printVersion bool
28-
29-
flagSet := flag.NewFlagSet("davcli-args", flag.ContinueOnError)
30-
flagSet.StringVar(&configFilePath, "c", "", "Config file path")
31-
flagSet.BoolVar(&printVersion, "v", false, "print version info")
22+
func (app *App) run(args []string) (err error) {
3223

33-
err = flagSet.Parse(args)
24+
err = app.runner.SetConfig(app.config)
3425
if err != nil {
26+
err = fmt.Errorf("Invalid CA Certificate: %s", err.Error()) //nolint:staticcheck
3527
return
3628
}
3729

38-
if printVersion {
39-
fmt.Println("davcli version [[version]]")
40-
return
41-
}
30+
err = app.runner.Run(args)
31+
return
32+
}
4233

43-
if configFilePath == "" {
44-
err = errors.New("Config file arg `-c` is missing") //nolint:staticcheck
45-
return
46-
}
34+
func (app *App) Put(sourceFilePath string, destinationObject string) error {
35+
return app.run([]string{"put", sourceFilePath, destinationObject})
36+
}
4737

48-
file, err := os.Open(configFilePath)
49-
if err != nil {
50-
return
51-
}
38+
func (app *App) Get(sourceObject string, dest string) error {
39+
return app.run([]string{"get", sourceObject, dest})
40+
}
5241

53-
configBytes, err := io.ReadAll(file)
54-
if err != nil {
55-
return
56-
}
42+
func (app *App) Delete(object string) error {
43+
return app.run([]string{"delete", object})
44+
}
5745

58-
config := davconfig.Config{}
59-
err = json.Unmarshal(configBytes, &config)
46+
func (app *App) Exists(object string) (bool, error) {
47+
err := app.run([]string{"exists", object})
6048
if err != nil {
61-
return
49+
return false, err
6250
}
51+
return true, nil
52+
}
6353

64-
err = app.runner.SetConfig(config)
54+
func (app *App) Sign(object string, action string, expiration time.Duration) (string, error) {
55+
err := app.run([]string{"sign", object, action, expiration.String()})
6556
if err != nil {
66-
err = fmt.Errorf("Invalid CA Certificate: %s", err.Error()) //nolint:staticcheck
67-
return
57+
return "", err
6858
}
59+
return "", nil
60+
}
6961

70-
err = app.runner.Run(args[2:])
71-
return
62+
func (app *App) List(prefix string) ([]string, error) {
63+
return nil, errors.New("not implemented")
64+
}
65+
66+
func (app *App) Copy(srcBlob string, dstBlob string) error {
67+
return errors.New("not implemented")
68+
}
69+
70+
func (app *App) Properties(dest string) error {
71+
return errors.New("not implemented")
72+
}
73+
74+
func (app *App) EnsureStorageExists() error {
75+
return errors.New("not implemented")
76+
}
77+
78+
func (app *App) DeleteRecursive(prefix string) error {
79+
return errors.New("not implemented")
7280
}

‎dav/app/app_test.go‎

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"os"
66
"path/filepath"
7+
"time"
78

89
. "github.com/onsi/ginkgo"
910
. "github.com/onsi/gomega"
@@ -42,11 +43,15 @@ func pathToFixture(file string) string {
4243
}
4344

4445
var _ = Describe("App", func() {
46+
4547
It("reads the CA cert from config", func() {
46-
runner := &FakeRunner{}
48+
configFile, _ := os.Open(pathToFixture("dav-cli-config-with-ca.json")) //nolint:errcheck
49+
defer configFile.Close() //nolint:errcheck
50+
davConfig, _ := davconf.NewFromReader(configFile) //nolint:errcheck
4751

48-
app := New(runner)
49-
err := app.Run([]string{"dav-cli", "-c", pathToFixture("dav-cli-config-with-ca.json"), "put", "localFile", "remoteFile"})
52+
runner := &FakeRunner{}
53+
app := New(runner, davConfig)
54+
err := app.Put("localFile", "remoteFile")
5055
Expect(err).ToNot(HaveOccurred())
5156

5257
expectedConfig := davconf.Config{
@@ -66,22 +71,30 @@ var _ = Describe("App", func() {
6671
})
6772

6873
It("returns error if CA Cert is invalid", func() {
74+
configFile, _ := os.Open(pathToFixture("dav-cli-config-with-ca.json")) //nolint:errcheck
75+
defer configFile.Close() //nolint:errcheck
76+
davConfig, _ := davconf.NewFromReader(configFile) //nolint:errcheck
77+
6978
runner := &FakeRunner{
7079
SetConfigErr: errors.New("invalid cert"),
7180
}
7281

73-
app := New(runner)
74-
err := app.Run([]string{"dav-cli", "-c", pathToFixture("dav-cli-config-with-ca.json"), "put", "localFile", "remoteFile"})
82+
app := New(runner, davConfig)
83+
err := app.Put("localFile", "remoteFile")
7584
Expect(err).To(HaveOccurred())
7685
Expect(err.Error()).To(ContainSubstring("Invalid CA Certificate: invalid cert"))
7786

7887
})
7988

8089
It("runs the put command", func() {
90+
configFile, _ := os.Open(pathToFixture("dav-cli-config.json")) //nolint:errcheck
91+
defer configFile.Close() //nolint:errcheck
92+
davConfig, _ := davconf.NewFromReader(configFile) //nolint:errcheck
93+
8194
runner := &FakeRunner{}
8295

83-
app := New(runner)
84-
err := app.Run([]string{"dav-cli", "-c", pathToFixture("dav-cli-config.json"), "put", "localFile", "remoteFile"})
96+
app := New(runner, davConfig)
97+
err := app.Put("localFile", "remoteFile")
8598
Expect(err).ToNot(HaveOccurred())
8699

87100
expectedConfig := davconf.Config{
@@ -96,29 +109,58 @@ var _ = Describe("App", func() {
96109
Expect(runner.RunArgs).To(Equal([]string{"put", "localFile", "remoteFile"}))
97110
})
98111

99-
It("returns error with no config argument", func() {
100-
runner := &FakeRunner{}
112+
It("returns error from the cmd runner", func() {
101113

102-
app := New(runner)
103-
err := app.Run([]string{"put", "localFile", "remoteFile"})
104-
Expect(err).To(HaveOccurred())
105-
Expect(err.Error()).To(ContainSubstring("Config file arg `-c` is missing"))
106-
})
107-
It("prints the version info with the -v flag", func() {
108-
runner := &FakeRunner{}
109-
app := New(runner)
110-
err := app.Run([]string{"dav-cli", "-v"})
111-
Expect(err).ToNot(HaveOccurred())
112-
})
114+
configFile, _ := os.Open(pathToFixture("dav-cli-config.json")) //nolint:errcheck
115+
defer configFile.Close() //nolint:errcheck
116+
davConfig, _ := davconf.NewFromReader(configFile) //nolint:errcheck
113117

114-
It("returns error from the cmd runner", func() {
115118
runner := &FakeRunner{
116119
RunErr: errors.New("fake-run-error"),
117120
}
118121

119-
app := New(runner)
120-
err := app.Run([]string{"dav-cli", "-c", pathToFixture("dav-cli-config.json"), "put", "localFile", "remoteFile"})
122+
app := New(runner, davConfig)
123+
err := app.Put("localFile", "remoteFile")
121124
Expect(err).To(HaveOccurred())
122125
Expect(err.Error()).To(ContainSubstring("fake-run-error"))
123126
})
127+
128+
Context("Checking functionalities", func() {
129+
// var app *App
130+
var davConfig davconf.Config
131+
BeforeEach(func() {
132+
133+
configFile, _ := os.Open(pathToFixture("dav-cli-config.json")) //nolint:errcheck
134+
defer configFile.Close() //nolint:errcheck
135+
davConfig, _ = davconf.NewFromReader(configFile) //nolint:errcheck
136+
})
137+
138+
It("Exists fails", func() {
139+
140+
runner := &FakeRunner{
141+
RunErr: errors.New("object does not exist"),
142+
}
143+
app := New(runner, davConfig)
144+
145+
exist, err := app.Exists("someObject") //nolint:errcheck
146+
147+
Expect(err.Error()).To(ContainSubstring("object does not exist"))
148+
Expect(exist).To(BeFalse())
149+
150+
})
151+
152+
It("Sign Fails", func() {
153+
runner := &FakeRunner{
154+
RunErr: errors.New("can't sign"),
155+
}
156+
157+
app := New(runner, davConfig)
158+
signedurl, err := app.Sign("someObject", "SomeObject", time.Second*100)
159+
Expect(signedurl).To(BeEmpty())
160+
Expect(err.Error()).To(ContainSubstring("can't sign"))
161+
162+
})
163+
164+
})
165+
124166
})

‎dav/config/config.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package config
22

3+
import (
4+
"encoding/json"
5+
"io"
6+
)
7+
38
type Config struct {
49
User string
510
Password string
@@ -16,3 +21,19 @@ type TLS struct {
1621
type Cert struct {
1722
CA string
1823
}
24+
25+
func NewFromReader(reader io.Reader) (Config, error) {
26+
config := Config{}
27+
28+
configBytes, err := io.ReadAll(reader)
29+
if err != nil {
30+
return config, err
31+
}
32+
33+
err = json.Unmarshal(configBytes, &config)
34+
if err != nil {
35+
return config, err
36+
}
37+
38+
return config, nil
39+
}

‎dav/main/dav.go‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎dav/main/main_suite_test.go‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎main.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func main() {
1515

1616
configPath := flag.String("c", "", "configuration path")
1717
showVer := flag.Bool("v", false, "version")
18-
storageType := flag.String("s", "s3", "storage type: azurebs|alioss|s3|gcs")
18+
storageType := flag.String("s", "s3", "storage type: azurebs|alioss|s3|gcs|dav")
1919
flag.Parse()
2020

2121
if *showVer {
@@ -27,11 +27,13 @@ func main() {
2727
if err != nil {
2828
log.Fatalln(err)
2929
}
30+
defer configFile.Close() //nolint:errcheck
3031

3132
client, err := storage.NewStorageClient(*storageType, configFile)
3233
if err != nil {
3334
log.Fatalln(err)
3435
}
36+
3537
sty := storage.NewStrategy(client)
3638

3739
nonFlagArgs := flag.Args()

0 commit comments

Comments
 (0)