gitlin/cmd/gothub/setup.go
Arya K aa94dcc60a add support for disabling proxying (#94)
fixes #45

Co-authored-by: Arya Kiran <arya@projectsegfau.lt>
Reviewed-on: https://codeberg.org/gothub/gothub/pulls/94
2023-05-16 12:42:17 +00:00

170 行
4.7 KiB
Go

package gothub
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var setupCmd = &cobra.Command{
Use: "setup",
Short: "Interactive GotHub instance setup wizard",
Long: `This command allows you to setup your GotHub instance in an interactive way. It will ask you a few questions and then set environment variables for you.`,
Run: func(cmd *cobra.Command, args []string) {
setup()
},
}
func init() {
rootCmd.AddCommand(setupCmd)
}
type Vars struct {
IPLogging bool
URLLogging bool
UserAgentLogging bool
DiagnosticLogging bool
PrivacyPolicy string
InstanceCountry string
InstanceProvider string
CloudflareStatus string
Proxying string
}
func setup() {
fmt.Println("GotHub setup wizard")
fmt.Println("This wizard will help you setup your GotHub instance.")
// Docker check
if os.Getenv("DOCKER") == "true" {
fmt.Println("You are running GotHub in Docker. This wizard will not work in Docker. Please set the environment variables manually.")
os.Exit(1)
}
// set up variables
vars := Vars{}
// ask user for information about IP logging
fmt.Print("Do you log IP addresses of users? (y/n) ")
var ipLogging string
fmt.Scanln(&ipLogging)
if ipLogging == "y" {
// create environment variable
vars.IPLogging = true
} else {
// create environment variable
vars.IPLogging = false
}
// ask user for information about URL request logging
fmt.Print("Do you log URL requests of users? (y/n) ")
var urlLogging string
fmt.Scanln(&urlLogging)
if urlLogging == "y" {
vars.URLLogging = true
} else {
vars.URLLogging = false
}
// ask user for information about User-Agent logging
fmt.Print("Do you log your users User-Agent? (y/n) ")
var uaLogging string
fmt.Scanln(&uaLogging)
if uaLogging == "y" {
vars.UserAgentLogging = true
} else {
vars.UserAgentLogging = false
}
// ask user for information about diagnostic information logging
fmt.Print("Do you log diagnostic information? (y/n) ")
var diagLogging string
fmt.Scanln(&diagLogging)
if diagLogging == "y" {
vars.DiagnosticLogging = true
} else {
vars.DiagnosticLogging = false
}
// ask user about privacy policy
fmt.Print("Do you have a privacy policy? (y/n) ")
var privacyPolicy string
fmt.Scanln(&privacyPolicy)
if privacyPolicy == "y" {
// ask user for privacy policy URL
fmt.Print("Please enter the URL of your privacy policy. ")
var privacyPolicyURL string
fmt.Scanln(&privacyPolicyURL)
vars.PrivacyPolicy = privacyPolicyURL
} else {
vars.PrivacyPolicy = ""
}
// ask user about the country they're hosting from
fmt.Print("Please enter the country you're hosting from. ")
var country string
fmt.Scanln(&country)
if country != "" {
vars.InstanceCountry = country
} else {
vars.InstanceCountry = ""
}
// ask user about their ISP/Hosting provider
fmt.Print("Please enter your ISP/Hosting provider. ")
var isp string
fmt.Scanln(&isp)
if isp != "" {
vars.InstanceProvider = isp
} else {
vars.InstanceProvider = ""
}
// ask user about Cloudflare status
fmt.Print("Are you using Cloudflare? (y/n) ")
var cloudflare string
fmt.Scanln(&cloudflare)
if cloudflare == "y" {
vars.CloudflareStatus = "true"
} else {
vars.CloudflareStatus = "false"
}
// ask user whether they want proxying enabled
fmt.Print("Do you want proxying enabled? (y/n) ")
var proxying string
fmt.Scanln(&proxying)
if proxying == "y" {
vars.Proxying = "true"
} else {
vars.Proxying = "false"
}
// save to .env file
fmt.Println("Saving environment variables to .env file...")
f, err := os.Create(".env")
if err != nil {
fmt.Println("Error creating .env file: ", err)
os.Exit(1)
}
defer f.Close()
// write to file. make it look nice
f.WriteString("# GotHub environment variables\n")
f.WriteString("# This file was generated by the setup wizard. You can edit it manually if you want.\n")
f.WriteString("GOTHUB_SETUP_COMPLETE=true\n")
f.WriteString("GOTHUB_IP_LOGGED=" + fmt.Sprint(vars.IPLogging) + "\n")
f.WriteString("GOTHUB_REQUEST_URL_LOGGED=" + fmt.Sprint(vars.URLLogging) + "\n")
f.WriteString("GOTHUB_USER_AGENT_LOGGED=" + fmt.Sprint(vars.UserAgentLogging) + "\n")
f.WriteString("GOTHUB_DIAGNOSTIC_INFO_LOGGED=" + fmt.Sprint(vars.DiagnosticLogging) + "\n")
f.WriteString("GOTHUB_INSTANCE_PRIVACY_POLICY=" + vars.PrivacyPolicy + "\n")
f.WriteString("GOTHUB_INSTANCE_COUNTRY=" + vars.InstanceCountry + "\n")
f.WriteString("GOTHUB_INSTANCE_PROVIDER=" + vars.InstanceProvider + "\n")
f.WriteString("GOTHUB_INSTANCE_CLOUDFLARE=" + vars.CloudflareStatus)
f.WriteString("GOTHUB_PROXYING_ENABLED=" + vars.Proxying)
println("All done! You can now start your GotHub instance with 'gothub serve'.")
println("You can review the environment variables with 'gothub env'.")
}