diff --git a/cmd/hoster_cli/commands/checkUpdates.go b/cmd/hoster_cli/commands/checkUpdates.go new file mode 100644 index 0000000..2def8b2 --- /dev/null +++ b/cmd/hoster_cli/commands/checkUpdates.go @@ -0,0 +1,64 @@ +package commands + +import ( + "context" + "fmt" + "hoster/internals/conf" + "io/fs" + "path/filepath" + "strings" + + dc "github.com/compose-spec/compose-go/v2/cli" + "github.com/urfave/cli/v2" +) + +func CmdCheckUpdated(c *cli.Context) error { + projectRoot := conf.App.MustString("projects.root") + + composeFiles := []string{} + + // Find compose files recursively + err := filepath.WalkDir(projectRoot, func(path string, d fs.DirEntry, err error) error { + if err != nil { + // Ignore errors on purpose. + return nil + } + + if d.IsDir() { + return nil + } + + fileName := d.Name() + + if strings.HasPrefix(fileName, "docker-compose") && (strings.HasSuffix(fileName, ".yml") || strings.HasSuffix(fileName, ".yaml")) { + composeFiles = append(composeFiles, path) + } + + return nil + }) + + if err != nil { + return err + } + + composeFilePath := composeFiles[0] + + options, err := dc.NewProjectOptions( + []string{composeFilePath}, + ) + + if err != nil { + return err + } + + project, err := dc.ProjectFromOptions(context.Background(), options) + if err != nil { + return err + } + + for srvName, srv := range project.AllServices() { + fmt.Printf("%s => %s\n", srvName, srv.Image) + } + + return nil +} diff --git a/cmd/hoster_cli/commands/new.go b/cmd/hoster_cli/commands/new.go new file mode 100644 index 0000000..83af048 --- /dev/null +++ b/cmd/hoster_cli/commands/new.go @@ -0,0 +1,117 @@ +package commands + +import ( + "fmt" + "hoster/internals/conf" + "hoster/internals/helpers" + "hoster/internals/templates" + "hoster/internals/types" + "os" + "path/filepath" + "slices" + + "github.com/gookit/goutil/fsutil" + "github.com/gookit/goutil/sysutil/cmdr" + "github.com/kataras/golog" + "github.com/urfave/cli/v2" +) + +func CmdNew(c *cli.Context) error { + name := c.String("name") + domain := c.String("domain") + projectRoot := conf.App.MustString("projects.root") + sitesAvail := conf.App.MustString("nginx.sitesAvailable") + sitesEnabled := conf.App.MustString("nginx.sitesEnabled") + nginxFile := filepath.Join(sitesAvail, name) + + projectFolder := filepath.Join(projectRoot, name) + + existingProjects, err := helpers.FindHostConfigs(sitesAvail) + if err != nil { + return err + } + + usedPorts := []int{} + for _, project := range existingProjects { + usedPorts = append(usedPorts, project.InternalPort) + } + + port := conf.App.Int("nginx.portRangeStart") + freePort := 0 + for port <= conf.App.Int("nginx.portRangeEnd") { + if !slices.Contains(usedPorts, port) && helpers.IsFreePort(port) { + freePort = port + break + } + port++ + } + + if freePort == 0 { + return fmt.Errorf("no free port available") + } + + if !fsutil.IsDir(projectFolder) { + golog.Infof("Creating project folder '%s'", projectFolder) + err := os.MkdirAll(projectFolder, 0755) + if err != nil { + return err + } + } + + if fsutil.IsFile(nginxFile) { + return fmt.Errorf("the nginx config file '%s' already exists", nginxFile) + } + + // Set owner and group of project folder + err = os.Chown(projectFolder, conf.App.Int("projects.owner"), conf.App.Int("projects.group")) + if err != nil { + return err + } + + project := types.Project{ + Name: name, + Domain: domain, + InternalPort: freePort, + } + + // Create nginx config file + err = templates.Execute("nginx", nginxFile, project) + + if err != nil { + return err + } + + // Symlink nginx config file + err = os.Symlink(nginxFile, filepath.Join(sitesEnabled, name)) + if err != nil { + return err + } + + // Execute certbot command: "systemctl stop nginx && sudo certbot certonly --standalone --preferred-challenges http -d {domain} && sudo systemctl start nginx" + + com := cmdr.NewCmd("systemctl"). + WithArgs([]string{"stop", "nginx"}) + + _, err = com.Output() + if err != nil { + return err + } + + com = cmdr.NewCmd("certbot"). + WithArgs([]string{"certonly", "--standalone", "--preferred-challenges", "http", "-d", domain}) + _, err = com.Output() + if err != nil { + return err + } + + com = cmdr.NewCmd("systemctl"). + WithArgs([]string{"start", "nginx"}) + _, err = com.Output() + if err != nil { + return err + } + + golog.Infof("Project successfully '%s' created. Uses port %d.", name, freePort) + + return nil +} diff --git a/cmd/hoster_cli/hoster.go b/cmd/hoster_cli/hoster.go index 0016ed4..98b0681 100644 --- a/cmd/hoster_cli/hoster.go +++ b/cmd/hoster_cli/hoster.go @@ -1,19 +1,12 @@ package main import ( - "fmt" + "hoster/cmd/hoster_cli/commands" "hoster/internals/conf" - "hoster/internals/helpers" - "hoster/internals/templates" - "hoster/internals/types" "os" - "path/filepath" - "github.com/gookit/goutil/fsutil" - "github.com/gookit/goutil/sysutil/cmdr" "github.com/kataras/golog" "github.com/urfave/cli/v2" - "golang.org/x/exp/slices" ) func main() { @@ -22,7 +15,7 @@ func main() { app := &cli.App{ Name: "hoster", Usage: "Hoster", - Version: "1.0.0", + Version: "1.0.1", Commands: []*cli.Command{ { Name: "new", @@ -41,96 +34,12 @@ func main() { Required: true, }, }, - Action: func(c *cli.Context) error { - name := c.String("name") - domain := c.String("domain") - projectRoot := conf.App.MustString("projects.root") - sitesAvail := conf.App.MustString("nginx.sitesAvailable") - sitesEnabled := conf.App.MustString("nginx.sitesEnabled") - nginxFile := filepath.Join(sitesAvail, name) - - projectFolder := filepath.Join(projectRoot, name) - - existingProjects, err := helpers.FindHostConfigs(sitesAvail) - if err != nil { - return err - } - - usedPorts := []int{} - for _, project := range existingProjects { - usedPorts = append(usedPorts, project.InternalPort) - } - - port := conf.App.Int("nginx.portRangeStart") - freePort := 0 - for port <= conf.App.Int("nginx.portRangeEnd") { - if !slices.Contains(usedPorts, port) { - freePort = port - break - } - port++ - } - - if freePort == 0 { - return fmt.Errorf("no free port available") - } - - if !fsutil.IsDir(projectFolder) { - golog.Infof("Creating project folder '%s'", projectFolder) - err := os.MkdirAll(projectFolder, 0755) - if err != nil { - return err - } - } - - if fsutil.IsFile(nginxFile) { - return fmt.Errorf("the nginx config file '%s' already exists", nginxFile) - } - - // Set owner and group of project folder - err = os.Chown(projectFolder, conf.App.Int("projects.owner"), conf.App.Int("projects.group")) - if err != nil { - return err - } - - project := types.Project{ - Name: name, - Domain: domain, - InternalPort: freePort, - } - - // Create nginx config file - err = templates.Execute("nginx", nginxFile, project) - - if err != nil { - return err - } - - // Symlink nginx config file - err = os.Symlink(nginxFile, filepath.Join(sitesEnabled, name)) - if err != nil { - return err - } - - // Execute certbot command: "systemctl stop nginx && sudo certbot certonly --standalone --preferred-challenges http -d {domain} && sudo systemctl start nginx" - - com := cmdr.NewCmd("systemctl"). - WithArgs([]string{"stop", "nginx"}) - out := com.SafeOutput() - fmt.Println(out) - - com = cmdr.NewCmd("certbot"). - WithArgs([]string{"certonly", "--standalone", "--preferred-challenges", "http", "-d", domain}) - out = com.SafeOutput() - fmt.Println(out) - - com = cmdr.NewCmd("systemctl"). - WithArgs([]string{"start", "nginx"}) - out = com.SafeOutput() - fmt.Println(out) - - return nil - }, + Action: commands.CmdNew, + }, + { + Name: "check", + Usage: "Check if newer versions of docker images are available", + Action: commands.CmdCheckUpdated, }, }, } diff --git a/go.mod b/go.mod index 8e2d0a3..cccca19 100644 --- a/go.mod +++ b/go.mod @@ -3,29 +3,41 @@ module hoster go 1.23.0 require ( + github.com/compose-spec/compose-go/v2 v2.1.6 github.com/gookit/config/v2 v2.2.5 github.com/gookit/goutil v0.6.16 github.com/kataras/golog v0.1.12 github.com/urfave/cli/v2 v2.27.4 - golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 ) require ( dario.cat/mergo v1.0.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect + github.com/distribution/reference v0.5.0 // indirect + github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-units v0.5.0 // indirect github.com/fatih/color v1.14.1 // indirect + github.com/go-viper/mapstructure/v2 v2.0.0 // indirect github.com/goccy/go-yaml v1.11.2 // indirect github.com/gookit/color v1.5.4 // indirect github.com/kataras/pio v0.0.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect + github.com/mattn/go-shellwords v1.0.12 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/sirupsen/logrus v1.9.0 // indirect + github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect + github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect + github.com/xeipuuv/gojsonschema v1.2.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect + golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.22.0 // indirect golang.org/x/term v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index aee000e..694528a 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,18 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +github.com/compose-spec/compose-go/v2 v2.1.6 h1:d0Cs0DffmOwmSzs0YPHwKCskknGq2jfGg4uGowlEpps= +github.com/compose-spec/compose-go/v2 v2.1.6/go.mod h1:lFN0DrMxIncJGYAXTfWuajfwj5haBJqrBkarHcnjJKc= github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0= +github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w= github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= @@ -12,6 +21,8 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/go-viper/mapstructure/v2 v2.0.0 h1:dhn8MZ1gZ0mzeodTG3jt5Vj/o87xZKuNAprG2mQfMfc= +github.com/go-viper/mapstructure/v2 v2.0.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/goccy/go-yaml v1.11.2 h1:joq77SxuyIs9zzxEjgyLBugMQ9NEgTWxXfz2wVqwAaQ= github.com/goccy/go-yaml v1.11.2/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= @@ -35,26 +46,42 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= +github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8= github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ= +github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= +github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= +github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= +github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= -golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 h1:hNQpMuAJe5CtcUqCXaWga3FHu+kQvCqcsoVaQgSV60o= +golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -64,6 +91,10 @@ golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= +gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g= diff --git a/internals/helpers/port.go b/internals/helpers/port.go new file mode 100644 index 0000000..978522e --- /dev/null +++ b/internals/helpers/port.go @@ -0,0 +1,20 @@ +package helpers + +import ( + "fmt" + "net" +) + +func IsFreePort(port int) bool { + addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", port)) + if err != nil { + return false + } + + l, err := net.ListenTCP("tcp", addr) + if err != nil { + return false + } + defer l.Close() + return true +}