Untitled

mail@pastecode.io avatar
unknown
golang
2 years ago
2.6 kB
2
Indexable
Never
if command == "transfer" {
		subArgs := flag.NewFlagSet(command, flag.ExitOnError)
		if argc == 7 {
			// EdgeCollector transfer <abspath to file> <datasource name> <environment> <bucket prefix> <target object>
			action.Transfer(os.Args[2], os.Args[3], os.Args[4], os.Args[5], os.Args[6])
			return
		}
		fileName := subArgs.String("file", "", "Source File Path")
		dataSourceName := subArgs.String("datasource", "", "Data Source Unique Name")
		environment := subArgs.String("environment", "", "Environment Name")
		prefix := subArgs.String("folder", "", "Cloud Destination path within the Data Hub")
		target := subArgs.String("target", "", "Transfer File Name with extension (e.g test.txt)")
		err := subArgs.Parse(os.Args[2:])
		if err != nil {
			logger.Error("subcommand: transfer error occurred. err:", err)
			return
		}
		complete := true
		subArgs.VisitAll(func(flag *flag.Flag) {
			if fmt.Sprintf("%v", flag.Value) == "" {
				complete = false
			}
		})
		if !complete {
			subArgs.Usage()
			fmt.Println("\nOr: EdgeCollector transfer <abspath to file> <datasource name> <environment> <destination folder> <target object>")
			os.Exit(1)
		}

		//Moving the filepath to the temp directory and writing the contents of the file to the temp dir
		tempdir := os.TempDir()
		edgeDir := filepath.Join(tempdir, "edge_collector")
		os.Mkdir(edgeDir, 0700)
		logger.Infof("tempdir: %s\nedgedir:", tempdir, edgeDir)
		ext := filepath.Ext(*fileName)
		fileextname := strings.Split(*fileName, `\`)
		fileNameWithoutExt := strings.Split(fileextname[len(fileextname) - 1], ".")
		nameNew := fmt.Sprintf("%s/%s", edgeDir, fileNameWithoutExt[0])

		//name is in format tempFolder/edege_collector/filename_unixnanoValue.fileExtension
		newFileDirectory := fmt.Sprintf("%s_%v%s", nameNew, (time.Now()).Unix(), ext)
		f, err := os.Create(newFileDirectory)
		if err != nil {
			err = fmt.Errorf("cannot create tmp file for single upload : %v", err)
			logger.Error(err)
			return
		}
		data, err := ioutil.ReadFile(*fileName)
		if err != nil {
			err = fmt.Errorf("cannot read the uploaded file : %v", err)
			logger.Error(err)
			return
		}
		err = ioutil.WriteFile(f.Name(), data, 0600)
		if err != nil {
			err = fmt.Errorf("cannot create tmp file for single upload : %v", err)
			logger.Error(err)
			return
		}
		f.Close()

		newFileDirectory = filepath.ToSlash(newFileDirectory)
		*prefix = filepath.ToSlash(*prefix)
		action.Transfer(newFileDirectory, *dataSourceName, *environment, *prefix, *target)
		return
	}