|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +func UpdateEnvs(envfile string, Dockerfile string) error { |
| 12 | + // open envfile in read mode |
| 13 | + file, err := os.OpenFile(envfile, os.O_RDONLY, 0644) |
| 14 | + if err != nil { |
| 15 | + return err |
| 16 | + } |
| 17 | + |
| 18 | + // read the content of envfile |
| 19 | + fileinfo, err := file.Stat() |
| 20 | + content := make([]byte, fileinfo.Size()) |
| 21 | + _, err = file.Read(content) |
| 22 | + if err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + |
| 26 | + // parse key value pairs from envfile |
| 27 | + envs := strings.Split(string(content), "\n") |
| 28 | + envKeys := make([]string, 0) |
| 29 | + for _, env := range envs { |
| 30 | + if env != "" || env != "\n" || env[0] != '#' { |
| 31 | + keyValue := strings.Split(env, "=") |
| 32 | + if len(keyValue) != 2 { |
| 33 | + continue |
| 34 | + } |
| 35 | + |
| 36 | + line := "ENV " + keyValue[0] + " {" + keyValue[0] + "}" |
| 37 | + |
| 38 | + envKeys = append(envKeys, line) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // open Dockerfile in read & write mode |
| 43 | + dockerfile, err := os.OpenFile(Dockerfile, os.O_RDWR, 0644) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + // search for {{.ENV}} in Dockerfile |
| 49 | + fileinfo, err = dockerfile.Stat() |
| 50 | + content = make([]byte, fileinfo.Size()) |
| 51 | + _, err = dockerfile.Read(content) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + // prepare keys to replace {{.ENV}} in Dockerfile |
| 57 | + replaceText := strings.Join(envKeys, "\n") |
| 58 | + replaceText = replaceText + "\n" |
| 59 | + replaceText = "\n\n# {{ENV}}\n" + replaceText + "# {{END ENV}}\n\n" |
| 60 | + |
| 61 | + // search for {{.ENV}} in Dockerfile |
| 62 | + |
| 63 | + re := regexp.MustCompile(`\n*#\s*{{ENV}}(\D|\d)*#\s*{{END ENV}}\n*`) |
| 64 | + match := string(re.Find(content)) |
| 65 | + |
| 66 | + if match == "" { |
| 67 | + log.Println("{{.ENV}} not used previously in Dockerfile") |
| 68 | + re = regexp.MustCompile(`\n*#\s*{{ENV}}`) |
| 69 | + match = string(re.Find(content)) |
| 70 | + fmt.Println(match) |
| 71 | + if match == "" { |
| 72 | + return fmt.Errorf("{{.ENV}} not found in Dockerfile") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // replace {{.ENV}} with key value pairs from envfile |
| 77 | + newContent := strings.Replace(string(content), match, replaceText, 1) |
| 78 | + |
| 79 | + // clear the contents of the Dockerfile |
| 80 | + err = dockerfile.Truncate(0) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + // write the new content to Dockerfile |
| 86 | + _, err = dockerfile.WriteAt([]byte(newContent), 0) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | + |
| 93 | +} |
0 commit comments