cmd/swarm: auto resolve default path according to env flag (#17960)
This commit is contained in:
parent
f08f596a37
commit
126dfde6c9
@ -80,6 +80,7 @@ const (
|
|||||||
SWARM_ENV_STORE_CAPACITY = "SWARM_STORE_CAPACITY"
|
SWARM_ENV_STORE_CAPACITY = "SWARM_STORE_CAPACITY"
|
||||||
SWARM_ENV_STORE_CACHE_CAPACITY = "SWARM_STORE_CACHE_CAPACITY"
|
SWARM_ENV_STORE_CACHE_CAPACITY = "SWARM_STORE_CACHE_CAPACITY"
|
||||||
SWARM_ACCESS_PASSWORD = "SWARM_ACCESS_PASSWORD"
|
SWARM_ACCESS_PASSWORD = "SWARM_ACCESS_PASSWORD"
|
||||||
|
SWARM_AUTO_DEFAULTPATH = "SWARM_AUTO_DEFAULTPATH"
|
||||||
GETH_ENV_DATADIR = "GETH_DATADIR"
|
GETH_ENV_DATADIR = "GETH_DATADIR"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,8 +26,10 @@ import (
|
|||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
|
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/cmd/utils"
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||||
@ -55,9 +57,16 @@ func upload(ctx *cli.Context) {
|
|||||||
mimeType = ctx.GlobalString(SwarmUploadMimeType.Name)
|
mimeType = ctx.GlobalString(SwarmUploadMimeType.Name)
|
||||||
client = swarm.NewClient(bzzapi)
|
client = swarm.NewClient(bzzapi)
|
||||||
toEncrypt = ctx.Bool(SwarmEncryptedFlag.Name)
|
toEncrypt = ctx.Bool(SwarmEncryptedFlag.Name)
|
||||||
|
autoDefaultPath = false
|
||||||
file string
|
file string
|
||||||
)
|
)
|
||||||
|
if autoDefaultPathString := os.Getenv(SWARM_AUTO_DEFAULTPATH); autoDefaultPathString != "" {
|
||||||
|
b, err := strconv.ParseBool(autoDefaultPathString)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("invalid environment variable %s: %v", SWARM_AUTO_DEFAULTPATH, err)
|
||||||
|
}
|
||||||
|
autoDefaultPath = b
|
||||||
|
}
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
if fromStdin {
|
if fromStdin {
|
||||||
tmp, err := ioutil.TempFile("", "swarm-stdin")
|
tmp, err := ioutil.TempFile("", "swarm-stdin")
|
||||||
@ -106,6 +115,15 @@ func upload(ctx *cli.Context) {
|
|||||||
if !recursive {
|
if !recursive {
|
||||||
return "", errors.New("Argument is a directory and recursive upload is disabled")
|
return "", errors.New("Argument is a directory and recursive upload is disabled")
|
||||||
}
|
}
|
||||||
|
if autoDefaultPath && defaultPath == "" {
|
||||||
|
defaultEntryCandidate := path.Join(file, "index.html")
|
||||||
|
log.Debug("trying to find default path", "path", defaultEntryCandidate)
|
||||||
|
defaultEntryStat, err := os.Stat(defaultEntryCandidate)
|
||||||
|
if err == nil && !defaultEntryStat.IsDir() {
|
||||||
|
log.Debug("setting auto detected default path", "path", defaultEntryCandidate)
|
||||||
|
defaultPath = defaultEntryCandidate
|
||||||
|
}
|
||||||
|
}
|
||||||
if defaultPath != "" {
|
if defaultPath != "" {
|
||||||
// construct absolute default path
|
// construct absolute default path
|
||||||
absDefaultPath, _ := filepath.Abs(defaultPath)
|
absDefaultPath, _ := filepath.Abs(defaultPath)
|
||||||
|
Loading…
Reference in New Issue
Block a user