Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
chart-server
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruce Flynn
chart-server
Commits
5b74f5c6
Commit
5b74f5c6
authored
6 years ago
by
Bruce Flynn
Browse files
Options
Downloads
Patches
Plain Diff
shore up err handling a bit
parent
ad0651e9
Branches
main
No related tags found
No related merge requests found
Pipeline
#4067
passed with stage
Stage: push
in 21 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.go
+21
-16
21 additions, 16 deletions
main.go
with
21 additions
and
16 deletions
main.go
+
21
−
16
View file @
5b74f5c6
...
...
@@ -13,6 +13,16 @@ import (
"github.com/spf13/pflag"
)
func
httpError
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
msg
string
,
code
int
)
{
log
.
Printf
(
"HttpError[%d %s]: %s"
,
code
,
msg
,
r
.
URL
)
http
.
Error
(
w
,
msg
,
code
)
}
func
httpFail
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
msg
string
,
err
error
)
{
log
.
Printf
(
"InternalServerError: %s: %s: %s"
,
msg
,
err
,
r
.
URL
)
http
.
Error
(
w
,
msg
,
http
.
StatusInternalServerError
)
}
func
updateIndex
()
error
{
cmd
:=
exec
.
Command
(
"helm"
,
"repo"
,
"index"
,
"--url"
,
url
,
dir
)
cmd
.
Stderr
=
os
.
Stderr
...
...
@@ -28,33 +38,29 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
st
,
err
:=
os
.
Stat
(
p
)
// got an error and the file exists... something interesting perhaps?
if
err
!=
nil
&&
!
os
.
IsNotExist
(
err
)
{
log
.
Printf
(
"ERROR: could not stat %s : %s
\n
"
,
p
,
err
)
http
.
Error
(
w
,
"InternalServerError"
,
http
.
StatusInternalServerError
)
httpError
(
w
,
r
,
"InternalServerError"
,
http
.
StatusInternalServerError
)
return
}
// got a stat result, file must exist
if
st
!=
nil
{
http
.
Error
(
w
,
"File exists"
,
http
.
StatusForbidden
)
httpError
(
w
,
r
,
"File exists"
,
http
.
StatusForbidden
)
return
}
// file does not exist, proceed with upload
f
,
err
:=
os
.
Create
(
p
)
if
err
!=
nil
{
log
.
Printf
(
"InternalServerErrror: could not create %s: %s
\n
"
,
p
,
err
)
http
.
Error
(
w
,
"Could not complete upload"
,
http
.
StatusInternalServerError
)
httpFail
(
w
,
r
,
"Could not complete upload"
,
err
)
return
}
if
_
,
err
:=
io
.
Copy
(
f
,
r
.
Body
);
err
!=
nil
{
log
.
Printf
(
"InternalServerErrror: could not copy %s: %s
\n
"
,
p
,
err
)
http
.
Error
(
w
,
"Could not complete upload"
,
http
.
StatusInternalServerError
)
httpFail
(
w
,
r
,
"Could not complete upload"
,
err
)
return
}
err
=
updateIndex
()
if
err
!=
nil
{
log
.
Printf
(
"ERROR updating index: %s
\n
"
,
err
)
http
.
Error
(
w
,
"Could not update index"
,
http
.
StatusInternalServerError
)
httpError
(
w
,
r
,
"Could not update index"
,
http
.
StatusInternalServerError
)
// best-effort to remove the file if we could not update the index
os
.
Remove
(
p
)
return
...
...
@@ -80,19 +86,18 @@ helm repo add <name> %s
func
handleGetFile
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
p
:=
path
.
Join
(
dir
,
r
.
URL
.
Path
)
if
st
,
err
:=
os
.
Stat
(
p
);
os
.
IsNotExist
(
err
)
||
st
.
IsDir
()
{
http
.
Error
(
w
,
"Not Found"
,
http
.
StatusNotFound
)
st
,
err
:=
os
.
Stat
(
p
)
if
err
!=
nil
&&
os
.
IsNotExist
(
err
)
||
st
!=
nil
&&
st
.
IsDir
()
{
httpError
(
w
,
r
,
"Not Found"
,
http
.
StatusNotFound
)
return
}
f
,
err
:=
os
.
Open
(
p
)
if
err
!=
nil
{
log
.
Printf
(
"InternalServerErrror: could not open %s: %s
\n
"
,
p
,
err
)
http
.
Error
(
w
,
"InternalServerError"
,
http
.
StatusInternalServerError
)
httpFail
(
w
,
r
,
"InternalServerError"
,
err
)
return
}
if
_
,
err
:=
io
.
Copy
(
w
,
f
);
err
!=
nil
{
log
.
Printf
(
"InternalServerErrror: could not copy %s: %s
\n
"
,
p
,
err
)
http
.
Error
(
w
,
"InternalServerError"
,
http
.
StatusInternalServerError
)
httpFail
(
w
,
r
,
"InternalServerError"
,
err
)
return
}
}
...
...
@@ -108,7 +113,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
handleGetFile
(
w
,
r
)
}
default
:
http
.
Error
(
w
,
"Bad Request"
,
http
.
StatusBadRequest
)
httpError
(
w
,
r
,
"Bad Request"
,
http
.
StatusBadRequest
)
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment