Skip to content

Commit

Permalink
feat: add generate ports
Browse files Browse the repository at this point in the history
  • Loading branch information
darkjinnee committed Mar 6, 2022
1 parent eedb6d3 commit 2731e7b
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions cmd/envporter/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package main

import (
"bufio"
"flag"
"fmt"
"github.com/darkjinnee/envporter/internal/app/envporter"
"github.com/darkjinnee/go-err"
"os"
"regexp"
"strconv"
"strings"
)

type Args struct {
File *os.File
Path string
Reg string
Min int
Max int
}
Expand All @@ -37,6 +41,12 @@ func init() {
9999,
"maximum port number range",
)
flag.StringVar(
&args.Reg,
"reg",
`\BPORT\b`,
"regular expression for substring search",
)
flag.Parse()

dirPath, err := os.Getwd()
Expand Down Expand Up @@ -64,8 +74,27 @@ func init() {
}

func main() {
for min, max := range map[int]int{6000: 7000, 8000: 9999} {
fmt.Print(envporter.FreePort(min, max))
fmt.Print("\n")
var content string
fileScanner := bufio.NewScanner(args.File)
for fileScanner.Scan() {
line := fileScanner.Text()
if ok, _ := regexp.MatchString(args.Reg, line); !ok {
content += strings.Join([]string{
line,
"\n",
}, "")
continue
}

port := envporter.FreePort(args.Min, args.Max)
lineArr := strings.Split(line, "=")
content += strings.Join([]string{
lineArr[0],
"=",
strconv.Itoa(port),
"\n",
}, "")
}

fmt.Print(content)
}

0 comments on commit 2731e7b

Please sign in to comment.