Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
H
helm3
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
helm3
Commits
b39a0848
Commit
b39a0848
authored
May 04, 2016
by
vaikas-google
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first cut of fetch
parent
eba4c59a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
7 deletions
+106
-7
fetch.go
cmd/helm/fetch.go
+53
-7
fetch_test.go
cmd/helm/fetch_test.go
+53
-0
No files found.
cmd/helm/fetch.go
View file @
b39a0848
...
...
@@ -4,8 +4,11 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"github.com/kubernetes/helm/pkg/repo"
"github.com/spf13/cobra"
)
...
...
@@ -14,23 +17,37 @@ func init() {
}
var
fetchCmd
=
&
cobra
.
Command
{
Use
:
"fetch"
,
Short
:
"Download a chart from a repository and unpack it in local directory."
,
Use
:
"fetch
[chart URL | repo/chartname]
"
,
Short
:
"Download a chart from a repository and
(optionally)
unpack it in local directory."
,
Long
:
""
,
RunE
:
fetch
,
}
func
fetch
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
// parse args
if
len
(
args
)
==
0
{
return
fmt
.
Errorf
(
"This command needs at least one argument, url or repo/name of the chart."
)
}
f
,
err
:=
repo
.
LoadRepositoriesFile
(
repositoriesFile
())
if
err
!=
nil
{
return
err
}
// get download url
// call download url
out
,
err
:=
os
.
Create
(
"nginx-2.0.0.tgz"
)
u
,
err
:=
mapRepoArg
(
args
[
0
],
f
.
Repositories
)
if
err
!=
nil
{
return
err
}
// Grab the package name that we'll use for the name of the file to download to.
p
:=
strings
.
Split
(
u
.
String
(),
"/"
)
chartName
:=
p
[
len
(
p
)
-
1
]
out
,
err
:=
os
.
Create
(
chartName
)
if
err
!=
nil
{
return
err
}
defer
out
.
Close
()
resp
,
err
:=
http
.
Get
(
"http://localhost:8879/charts/nginx-2.0.0.tgz"
)
fmt
.
Println
(
"after req"
)
resp
,
err
:=
http
.
Get
(
u
)
// unpack file
if
err
!=
nil
{
return
err
...
...
@@ -44,3 +61,32 @@ func fetch(cmd *cobra.Command, args []string) error {
}
return
nil
}
// mapRepoArg figures out which format the argument is given, and creates a fetchable
// url from it.
func
mapRepoArg
(
arg
string
,
r
map
[
string
]
string
)
(
*
url
.
URL
,
error
)
{
// See if it's already a full URL.
u
,
err
:=
url
.
ParseRequestURI
(
arg
)
if
err
==
nil
{
// If it has a scheme and host and path, it's a full URL
if
u
.
IsAbs
()
&&
len
(
u
.
Host
)
>
0
&&
len
(
u
.
Path
)
>
0
{
return
u
,
nil
}
else
{
return
nil
,
fmt
.
Errorf
(
"Invalid chart url format: %s"
,
arg
)
}
}
// See if it's of the form: repo/path_to_chart
p
:=
strings
.
Split
(
arg
,
"/"
)
if
len
(
p
)
>
1
{
if
baseUrl
,
ok
:=
r
[
p
[
0
]];
ok
{
if
!
strings
.
HasSuffix
(
baseUrl
,
"/"
)
{
baseUrl
=
baseUrl
+
"/"
}
return
url
.
ParseRequestURI
(
baseUrl
+
strings
.
Join
(
p
[
1
:
],
"/"
))
}
else
{
return
nil
,
fmt
.
Errorf
(
"No such repo: %s"
,
p
[
0
])
}
}
else
{
return
nil
,
fmt
.
Errorf
(
"Invalid chart url format: %s"
,
arg
)
}
}
cmd/helm/fetch_test.go
0 → 100644
View file @
b39a0848
package
main
import
(
"fmt"
// "io"
// "net/http"
//"net/url"
// "os"
"testing"
)
type
testCase
struct
{
in
string
expectedErr
error
expectedOut
string
}
var
repos
=
map
[
string
]
string
{
"local"
:
"http://localhost:8879/charts"
,
"someother"
:
"http://storage.googleapis.com/mycharts"
,
}
var
testCases
=
[]
testCase
{
{
"bad"
,
fmt
.
Errorf
(
"Invalid chart url format: bad"
),
""
},
{
"http://"
,
fmt
.
Errorf
(
"Invalid chart url format: http://"
),
""
},
{
"http://example.com"
,
fmt
.
Errorf
(
"Invalid chart url format: http://example.com"
),
""
},
{
"http://example.com/foo/bar"
,
nil
,
"http://example.com/foo/bar"
},
{
"local/nginx-2.0.0.tgz"
,
nil
,
"http://localhost:8879/charts/nginx-2.0.0.tgz"
},
{
"nonexistentrepo/nginx-2.0.0.tgz"
,
fmt
.
Errorf
(
"No such repo: nonexistentrepo"
),
""
},
}
func
testRunner
(
t
*
testing
.
T
,
tc
testCase
)
{
u
,
err
:=
mapRepoArg
(
tc
.
in
,
repos
)
if
(
tc
.
expectedErr
==
nil
&&
err
!=
nil
)
||
(
tc
.
expectedErr
!=
nil
&&
err
==
nil
)
||
(
tc
.
expectedErr
!=
nil
&&
err
!=
nil
&&
tc
.
expectedErr
.
Error
()
!=
err
.
Error
())
{
t
.
Errorf
(
"Expected mapRepoArg to fail with input %s %v but got %v"
,
tc
.
in
,
tc
.
expectedErr
,
err
)
}
if
(
u
==
nil
&&
len
(
tc
.
expectedOut
)
!=
0
)
||
(
u
!=
nil
&&
len
(
tc
.
expectedOut
)
==
0
)
||
(
u
!=
nil
&&
tc
.
expectedOut
!=
u
.
String
())
{
t
.
Errorf
(
"Expected %s to map to fetch url %v but got %v"
,
tc
.
in
,
tc
.
expectedOut
,
u
)
}
}
func
TestMappings
(
t
*
testing
.
T
)
{
for
_
,
tc
:=
range
testCases
{
testRunner
(
t
,
tc
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment