|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type UploadArtifact struct { |
| 10 | + File os.File |
| 11 | + Name string |
| 12 | +} |
| 13 | + |
| 14 | +type UploadAsset struct { |
| 15 | + File os.File |
| 16 | + Name string |
| 17 | + IsZip bool |
| 18 | +} |
| 19 | + |
| 20 | +func GetFilesAndInfos(uploadArtifacts *string) (artifacts []UploadArtifact, err error) { |
| 21 | + artifactsToUpload := strings.Split(*uploadArtifacts, ",") |
| 22 | + for _, artifact := range artifactsToUpload { |
| 23 | + var sanFilename string |
| 24 | + if strings.HasPrefix(artifact, "file=") { |
| 25 | + sanFilename = artifact[5:] |
| 26 | + } |
| 27 | + file, err := os.OpenFile(sanFilename, os.O_RDWR, 0644) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + info, _ := file.Stat() |
| 32 | + artifacts = append(artifacts, UploadArtifact{ |
| 33 | + File: *file, |
| 34 | + Name: info.Name(), |
| 35 | + }) |
| 36 | + } |
| 37 | + return |
| 38 | +} |
| 39 | + |
| 40 | +func GetAsstes(uploadArtifacts *string, check bool) (asset []UploadAsset, err error) { |
| 41 | + artifactsToUpload := strings.Split(*uploadArtifacts, ",") |
| 42 | + for _, artifact := range artifactsToUpload { |
| 43 | + assetName := strings.Split(artifact, "=") |
| 44 | + |
| 45 | + switch assetName[0] { |
| 46 | + case "file": |
| 47 | + file, err := os.OpenFile(assetName[1], os.O_RDWR, 0644) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + info, err := file.Stat() |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + // if check, then close file, else return file interface for upload |
| 56 | + if check { |
| 57 | + file.Close() |
| 58 | + } else { |
| 59 | + asset = append(asset, UploadAsset{ |
| 60 | + File: *file, |
| 61 | + Name: info.Name(), |
| 62 | + IsZip: false, |
| 63 | + }) |
| 64 | + } |
| 65 | + case "files": |
| 66 | + // todo |
| 67 | + case "zip": |
| 68 | + // todo |
| 69 | + default: |
| 70 | + return nil, fmt.Errorf("not an valid asset format") |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return |
| 75 | +} |
| 76 | + |
| 77 | +func CheckIsFile(name string) (body string, err error) { |
| 78 | + data, err := os.ReadFile(name) |
| 79 | + if err != nil { |
| 80 | + return "", err |
| 81 | + } |
| 82 | + return string(data), nil |
| 83 | +} |
0 commit comments