cmd/swarm: auto resolve default path according to env flag (#17960)
This commit is contained in:
		| @@ -80,6 +80,7 @@ const ( | ||||
| 	SWARM_ENV_STORE_CAPACITY          = "SWARM_STORE_CAPACITY" | ||||
| 	SWARM_ENV_STORE_CACHE_CAPACITY    = "SWARM_STORE_CACHE_CAPACITY" | ||||
| 	SWARM_ACCESS_PASSWORD             = "SWARM_ACCESS_PASSWORD" | ||||
| 	SWARM_AUTO_DEFAULTPATH            = "SWARM_AUTO_DEFAULTPATH" | ||||
| 	GETH_ENV_DATADIR                  = "GETH_DATADIR" | ||||
| ) | ||||
|  | ||||
|   | ||||
| @@ -26,8 +26,10 @@ import ( | ||||
| 	"os/user" | ||||
| 	"path" | ||||
| 	"path/filepath" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/ethereum/go-ethereum/log" | ||||
| 	swarm "github.com/ethereum/go-ethereum/swarm/api/client" | ||||
|  | ||||
| 	"github.com/ethereum/go-ethereum/cmd/utils" | ||||
| @@ -47,17 +49,24 @@ var upCommand = cli.Command{ | ||||
| func upload(ctx *cli.Context) { | ||||
| 	args := ctx.Args() | ||||
| 	var ( | ||||
| 		bzzapi       = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") | ||||
| 		recursive    = ctx.GlobalBool(SwarmRecursiveFlag.Name) | ||||
| 		wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name) | ||||
| 		defaultPath  = ctx.GlobalString(SwarmUploadDefaultPath.Name) | ||||
| 		fromStdin    = ctx.GlobalBool(SwarmUpFromStdinFlag.Name) | ||||
| 		mimeType     = ctx.GlobalString(SwarmUploadMimeType.Name) | ||||
| 		client       = swarm.NewClient(bzzapi) | ||||
| 		toEncrypt    = ctx.Bool(SwarmEncryptedFlag.Name) | ||||
| 		file         string | ||||
| 		bzzapi          = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") | ||||
| 		recursive       = ctx.GlobalBool(SwarmRecursiveFlag.Name) | ||||
| 		wantManifest    = ctx.GlobalBoolT(SwarmWantManifestFlag.Name) | ||||
| 		defaultPath     = ctx.GlobalString(SwarmUploadDefaultPath.Name) | ||||
| 		fromStdin       = ctx.GlobalBool(SwarmUpFromStdinFlag.Name) | ||||
| 		mimeType        = ctx.GlobalString(SwarmUploadMimeType.Name) | ||||
| 		client          = swarm.NewClient(bzzapi) | ||||
| 		toEncrypt       = ctx.Bool(SwarmEncryptedFlag.Name) | ||||
| 		autoDefaultPath = false | ||||
| 		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 fromStdin { | ||||
| 			tmp, err := ioutil.TempFile("", "swarm-stdin") | ||||
| @@ -106,6 +115,15 @@ func upload(ctx *cli.Context) { | ||||
| 			if !recursive { | ||||
| 				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 != "" { | ||||
| 				// construct absolute default path | ||||
| 				absDefaultPath, _ := filepath.Abs(defaultPath) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user