Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
485748c4
Commit
485748c4
authored
Dec 13, 2016
by
zelig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cmd/swarm: improve uploader output and add defaultpath option
parent
080699f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
22 deletions
+68
-22
main.go
cmd/swarm/main.go
+5
-0
upload.go
cmd/swarm/upload.go
+63
-22
No files found.
cmd/swarm/main.go
View file @
485748c4
...
...
@@ -108,6 +108,10 @@ var (
Name
:
"manifest"
,
Usage
:
"Automatic manifest upload"
,
}
SwarmUploadDefaultPath
=
cli
.
StringFlag
{
Name
:
"defaultpath"
,
Usage
:
"path to file served for empty url path (none)"
,
}
)
func
init
()
{
...
...
@@ -179,6 +183,7 @@ Prints the swarm hash of file or directory.
SwarmApiFlag
,
SwarmRecursiveUploadFlag
,
SwarmWantManifestFlag
,
SwarmUploadDefaultPath
,
}
app
.
Flags
=
append
(
app
.
Flags
,
debug
.
Flags
...
)
app
.
Before
=
func
(
ctx
*
cli
.
Context
)
error
{
...
...
cmd/swarm/upload.go
View file @
485748c4
...
...
@@ -27,6 +27,8 @@ import (
"mime"
"net/http"
"os"
"os/user"
"path"
"path/filepath"
"strings"
...
...
@@ -39,6 +41,7 @@ func upload(ctx *cli.Context) {
bzzapi
=
strings
.
TrimRight
(
ctx
.
GlobalString
(
SwarmApiFlag
.
Name
),
"/"
)
recursive
=
ctx
.
GlobalBool
(
SwarmRecursiveUploadFlag
.
Name
)
wantManifest
=
ctx
.
GlobalBoolT
(
SwarmWantManifestFlag
.
Name
)
defaultPath
=
ctx
.
GlobalString
(
SwarmUploadDefaultPath
.
Name
)
)
if
len
(
args
)
!=
1
{
log
.
Fatal
(
"need filename as the first and only argument"
)
...
...
@@ -48,8 +51,9 @@ func upload(ctx *cli.Context) {
file
=
args
[
0
]
client
=
&
client
{
api
:
bzzapi
}
mroot
manifest
entry
manifestEntry
)
fi
,
err
:=
os
.
Stat
(
file
)
fi
,
err
:=
os
.
Stat
(
expandPath
(
file
)
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
...
...
@@ -57,28 +61,49 @@ func upload(ctx *cli.Context) {
if
!
recursive
{
log
.
Fatal
(
"argument is a directory and recursive upload is disabled"
)
}
mroot
,
err
=
client
.
uploadDirectory
(
file
)
mroot
,
err
=
client
.
uploadDirectory
(
file
,
defaultPath
)
}
else
{
mroot
,
err
=
client
.
uploadFile
(
file
,
fi
)
if
wantManifest
{
// Wrap the raw file entry in a proper manifest so both hashes get printed.
mroot
=
manifest
{
Entries
:
[]
manifest
{
mroot
}}
}
entry
,
err
=
client
.
uploadFile
(
file
,
fi
)
mroot
=
manifest
{[]
manifestEntry
{
entry
}}
}
if
err
!=
nil
{
log
.
Fatalln
(
"upload failed:"
,
err
)
}
if
wantManifest
{
hash
,
err
:=
client
.
uploadManifest
(
mroot
)
if
err
!=
nil
{
log
.
Fatalln
(
"manifest upload failed:"
,
err
)
if
!
wantManifest
{
// Print the manifest. This is the only output to stdout.
mrootJSON
,
_
:=
json
.
MarshalIndent
(
mroot
,
""
,
" "
)
fmt
.
Println
(
string
(
mrootJSON
))
return
}
hash
,
err
:=
client
.
uploadManifest
(
mroot
)
if
err
!=
nil
{
log
.
Fatalln
(
"manifest upload failed:"
,
err
)
}
fmt
.
Println
(
hash
)
}
// Expands a file path
// 1. replace tilde with users home dir
// 2. expands embedded environment variables
// 3. cleans the path, e.g. /a/b/../c -> /a/c
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
func
expandPath
(
p
string
)
string
{
if
strings
.
HasPrefix
(
p
,
"~/"
)
||
strings
.
HasPrefix
(
p
,
"~
\\
"
)
{
if
home
:=
homeDir
();
home
!=
""
{
p
=
home
+
p
[
1
:
]
}
mroot
.
Hash
=
hash
}
return
path
.
Clean
(
os
.
ExpandEnv
(
p
))
}
// Print the manifest. This is the only output to stdout.
mrootJSON
,
_
:=
json
.
MarshalIndent
(
mroot
,
""
,
" "
)
fmt
.
Println
(
string
(
mrootJSON
))
func
homeDir
()
string
{
if
home
:=
os
.
Getenv
(
"HOME"
);
home
!=
""
{
return
home
}
if
usr
,
err
:=
user
.
Current
();
err
==
nil
{
return
usr
.
HomeDir
}
return
""
}
// client wraps interaction with the swarm HTTP gateway.
...
...
@@ -86,25 +111,41 @@ type client struct {
api
string
}
// manifest is the JSON representation of a swarm manifest.
type
manifestEntry
struct
{
Hash
string
`json:"hash,omitempty"`
ContentType
string
`json:"contentType,omitempty"`
Path
string
`json:"path,omitempty"`
}
// manifest is the JSON representation of a swarm manifest.
type
manifest
struct
{
Hash
string
`json:"hash,omitempty"`
ContentType
string
`json:"contentType,omitempty"`
Path
string
`json:"path,omitempty"`
Entries
[]
manifest
`json:"entries,omitempty"`
Entries
[]
manifestEntry
`json:"entries,omitempty"`
}
func
(
c
*
client
)
uploadFile
(
file
string
,
fi
os
.
FileInfo
)
(
manifest
,
error
)
{
func
(
c
*
client
)
uploadFile
(
file
string
,
fi
os
.
FileInfo
)
(
manifest
Entry
,
error
)
{
hash
,
err
:=
c
.
uploadFileContent
(
file
,
fi
)
m
:=
manifest
{
m
:=
manifest
Entry
{
Hash
:
hash
,
ContentType
:
mime
.
TypeByExtension
(
filepath
.
Ext
(
fi
.
Name
())),
}
return
m
,
err
}
func
(
c
*
client
)
uploadDirectory
(
dir
string
)
(
manifest
,
error
)
{
func
(
c
*
client
)
uploadDirectory
(
dir
string
,
defaultPath
string
)
(
manifest
,
error
)
{
dirm
:=
manifest
{}
if
len
(
defaultPath
)
>
0
{
fi
,
err
:=
os
.
Stat
(
defaultPath
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
entry
,
err
:=
c
.
uploadFile
(
defaultPath
,
fi
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
entry
.
Path
=
""
dirm
.
Entries
=
append
(
dirm
.
Entries
,
entry
)
}
prefix
:=
filepath
.
ToSlash
(
filepath
.
Clean
(
dir
))
+
"/"
err
:=
filepath
.
Walk
(
dir
,
func
(
path
string
,
fi
os
.
FileInfo
,
err
error
)
error
{
if
err
!=
nil
||
fi
.
IsDir
()
{
...
...
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