Skip to content

Commit

Permalink
support profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Taufik committed Nov 24, 2020
1 parent c0af962 commit c968313
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import (
var ports map[string]string

func main() {
readPorts()
profile := ""
if len(os.Args) > 1 {
profile = os.Args[1]
fmt.Println("Profile " + profile)
}
readPorts(profile)

serverMuxA := http.NewServeMux()
serverMuxA.HandleFunc("/", defaultHandler)
Expand All @@ -34,8 +39,18 @@ func main() {
http.ListenAndServe(":1000", serverMuxA)
}

func readPorts(){
file, err := os.Open("ports.txt")
func readPorts(profile string){
fileName := "ports.txt"
if len(strings.TrimSpace(profile)) > 0 {
fileName = "ports-"+profile+".txt"
if !fileExists(fileName) {
fmt.Println("File " + fileName + " not exist")
fmt.Println("Open default file ports.txt")
fileName = "ports.txt"
}
}

file, err := os.Open(fileName)
fatal(err)
defer file.Close()
scanner := bufio.NewScanner(file)
Expand Down Expand Up @@ -116,4 +131,12 @@ func copyHeader(source http.Header, dest *http.Header){
dest.Add(n, vv)
}
}
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

0 comments on commit c968313

Please sign in to comment.