package main
import (
"fmt"
"os"
)
func rm ( args [ ] string ) {
status , err := getStatus ( false )
if err != nil {
fmt . Printf ( "\nError: %s\n\n" , err )
os . Exit ( 1 )
return
}
if status . NeedsAPIToken {
fmt . Print ( "\nGreenhouse Daemon is not registered to a Greenhouse account yet. Run the following command to register your account:\n\ngreenhouse register GREENHOUSE_API_TOKEN [SERVER_NAME]\n\n" )
os . Exit ( 0 )
return
} else {
matchedTunnelIndexes := map [ int ] bool { }
matchedArgumentIndexes := map [ int ] bool { }
suggestMatch := map [ int ] int { }
for i , tunnel := range status . GUITunnels {
listenURL := tunnelListenURL ( & tunnel )
for j , arg := range args {
if arg == "all" || listenURL == arg {
matchedTunnelIndexes [ i ] = true
matchedArgumentIndexes [ j ] = true
}
if levenshteinDistance ( listenURL , arg ) < 5 {
suggestMatch [ j ] = i
}
}
}
fmt . Print ( "\n" )
anyUnmatchedArgs := false
for j , arg := range args {
if ! matchedArgumentIndexes [ j ] {
fmt . Printf ( "'%s' did not match the listen url of any configured tunnel." , arg )
suggestedMatch , hasSuggestedMatch := suggestMatch [ j ]
if hasSuggestedMatch {
suggestedMatchTunnel := status . GUITunnels [ suggestedMatch ]
fmt . Printf ( "\nDid you mean '%s'?" , tunnelListenURL ( & suggestedMatchTunnel ) )
}
fmt . Print ( "\n" )
anyUnmatchedArgs = true
}
}
if anyUnmatchedArgs {
fmt . Print ( "\nTo display the currently configured tunnels, run greenhouse ls\n\n" )
os . Exit ( 1 )
return
}
toSendToDaemon := [ ] GUITunnel { }
for i , tunnel := range status . GUITunnels {
if ! matchedTunnelIndexes [ i ] {
toSendToDaemon = append ( toSendToDaemon , tunnel )
} else {
fmt . Printf ( "Removing %s\n" , tunnelListenURL ( & tunnel ) )
}
}
saveConfiguration ( toSendToDaemon )
count := len ( matchedTunnelIndexes )
plural := ""
if count > 1 {
plural = "s"
}
fmt . Printf ( "\n%d tunnel%s removed successfully!\n\n" , count , plural )
}
}