diff --git a/cmd/root.go b/cmd/root.go index c7ac9ad..5cf8af7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,6 +16,7 @@ package cmd import ( + "fmt" "os" "github.com/mattrbianchi/twig" @@ -48,10 +49,11 @@ func init() { viper.SetEnvPrefix(flags.EnvPrefix) viper.AutomaticEnv() + flags.BinaryName = "fusera" } var rootCmd = &cobra.Command{ - Use: "fusera", + Use: flags.BinaryName, Short: "A FUSE interface to the NCBI Sequence Read Archive (SRA) - " + flags.Version, Long: ``, Version: flags.Version, @@ -60,6 +62,10 @@ var rootCmd = &cobra.Command{ // Execute runs the main command of fusera, which has no action of its own, // so it evaluates which subcommand should be executed. func Execute() { + if os.Geteuid() == 0 { + fmt.Println("Running Fusera as root is not supported. This causes problems with mounting the filesystem using FUSE.") + os.Exit(1) + } if err := rootCmd.Execute(); err != nil { prettyPrintError(err) os.Exit(1) diff --git a/flags/flags.go b/flags/flags.go index 032a913..c3f3ad4 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -14,6 +14,8 @@ var ( EnvPrefix = "dbgap" // Version should be set at compile time to `git describe --tags --abbrev=0` Version string + // BinaryName should be set on init in order to know what binary is using the flags library. + BinaryName string LocationName = "location" AccessionName = "accession" @@ -50,10 +52,10 @@ var ( EndpointMsg = "ADVANCED: Change the endpoint used to communicate with SDL API.\nEnvironment Variable: [$DBGAP_ENDPOINT]" AwsBatchMsg = "ADVANCED: Adjust the amount of accessions put in one request to the SDL API when using an AWS location.\nEnvironment Variable: [$DBGAP_AWS-BATCH]" GcpBatchMsg = "ADVANCED: Adjust the amount of accessions put in one request to the SDL API when using a GCP location.\nEnvironment Variable: [$DBGAP_GCP-BATCH]" - AwsProfileMsg = "The desired AWS credentials profile in ~/.aws/credentials to use for instances when files require the requester (you) to pay for accessing the file.\nEnvironment Variable: [$DBGAP_AWS-PROFILE]\nNOTE: This account will be charged all cost accrued by accessing these certain files through fusera." - GcpProfileMsg = "The desired GCP credentials profile in ~/.aws/credentials to use for instances when files require the requester (you) to pay for accessing the file.\nEnvironment Variable: [$DBGAP_GCP-PROFILE]\nNOTE: This account will be charged all cost accrued by accessing these certain files through fusera. These credentials should be in the AWS supported format that Google provides in order to work with their AWS compatible API." - SilentMsg = "Fusera prints nothing, most useful for using fusera in scripts." - VerboseMsg = "Fusera prints everything, most useful for troubleshooting." + AwsProfileMsg = "The desired AWS credentials profile in ~/.aws/credentials to use for instances when files require the requester (you) to pay for accessing the file.\nEnvironment Variable: [$DBGAP_AWS-PROFILE]\nNOTE: This account will be charged all cost accrued by accessing these certain files." + GcpProfileMsg = "The desired GCP credentials profile in ~/.aws/credentials to use for instances when files require the requester (you) to pay for accessing the file.\nEnvironment Variable: [$DBGAP_GCP-PROFILE]\nNOTE: This account will be charged all cost accrued by accessing these certain files. These credentials should be in the AWS supported format that Google provides in order to work with their AWS compatible API." + SilentMsg = "Prints nothing, most useful when running in scripts." + VerboseMsg = "Prints everything, most useful for troubleshooting." ) // ResolveLocation attempts to resolve the location on GCP and AWS. diff --git a/sdl/sdl.go b/sdl/sdl.go index 89387c6..a815ec2 100644 --- a/sdl/sdl.go +++ b/sdl/sdl.go @@ -93,6 +93,7 @@ func (c *Client) makeRequest(accessions []string, meta bool) ([]*fuseralib.Acces if err != nil { return nil, errors.New("can't create request to SDL API") } + req.Header.Set("User-Agent", flags.BinaryName+"-"+flags.Version) req.Header.Set("Content-Type", writer.FormDataContentType()) if flags.Verbose { reqdump, err := httputil.DumpRequestOut(req, true) diff --git a/sracp/cmd/root.go b/sracp/cmd/root.go index 291a58e..8fa9b48 100644 --- a/sracp/cmd/root.go +++ b/sracp/cmd/root.go @@ -93,10 +93,12 @@ func init() { viper.SetEnvPrefix("dbgap") viper.AutomaticEnv() + + flags.BinaryName = "sracp" } var rootCmd = &cobra.Command{ - Use: "sracp", + Use: flags.BinaryName, Short: "A tool similar to cp that allows a user to download accessions - " + flags.Version, Long: ``, Version: flags.Version, @@ -148,6 +150,12 @@ var rootCmd = &cobra.Command{ } } path := args[0] + // Test whether we can write to this location. If not, fail here. + err = os.MkdirAll(filepath.Join(path, ".test"), 0755) + if err != nil { + fmt.Printf("It seems like sracp cannot make directories under %s. Please check that you have correct permissions to write to that path.\n", path) + os.Exit(1) + } batch := flags.ResolveBatch(platform.Name, awsBatch, gcpBatch) var accessions []*fuseralib.Accession @@ -298,6 +306,10 @@ var rootCmd = &cobra.Command{ // Execute runs the root command of sracp, which copies files from the cloud to a local file system. func Execute() { + if os.Geteuid() == 0 { + fmt.Println("Running sracp as root is not supported. The tool should not require root.") + os.Exit(1) + } if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1)