2023-04-02 01:53:44 +09:00
package gothub
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var setupCmd = & cobra . Command {
Use : "setup" ,
Short : "Interactive GotHub instance setup wizard" ,
2023-04-02 05:03:15 +09:00
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. ` ,
2023-04-02 01:53:44 +09:00
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
}
func setup ( ) {
2023-04-02 04:56:06 +09:00
fmt . Println ( "GotHub setup wizard" )
2023-04-02 01:53:44 +09:00
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
2023-04-02 23:22:54 +09:00
fmt . Print ( "Do you log IP addresses of users? (y/n) " )
2023-04-02 01:53:44 +09:00
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
2023-04-02 23:22:54 +09:00
fmt . Print ( "Do you log URL requests of users? (y/n) " )
2023-04-02 01:53:44 +09:00
var urlLogging string
fmt . Scanln ( & urlLogging )
if urlLogging == "y" {
vars . URLLogging = true
} else {
vars . URLLogging = false
}
// ask user for information about User-Agent logging
2023-04-02 23:22:54 +09:00
fmt . Print ( "Do you log your users User-Agent? (y/n) " )
2023-04-02 01:53:44 +09:00
var uaLogging string
fmt . Scanln ( & uaLogging )
if uaLogging == "y" {
vars . UserAgentLogging = true
} else {
vars . UserAgentLogging = false
}
// ask user for information about diagnostic information logging
2023-04-02 23:22:54 +09:00
fmt . Print ( "Do you log diagnostic information? (y/n) " )
2023-04-02 01:53:44 +09:00
var diagLogging string
fmt . Scanln ( & diagLogging )
if diagLogging == "y" {
vars . DiagnosticLogging = true
} else {
vars . DiagnosticLogging = false
}
// ask user about privacy policy
2023-04-02 23:22:54 +09:00
fmt . Print ( "Do you have a privacy policy? (y/n) " )
2023-04-02 01:53:44 +09:00
var privacyPolicy string
fmt . Scanln ( & privacyPolicy )
if privacyPolicy == "y" {
// ask user for privacy policy URL
2023-04-02 23:22:54 +09:00
fmt . Print ( "Please enter the URL of your privacy policy. " )
2023-04-02 01:53:44 +09:00
var privacyPolicyURL string
fmt . Scanln ( & privacyPolicyURL )
vars . PrivacyPolicy = privacyPolicyURL
} else {
vars . PrivacyPolicy = ""
}
// ask user about the country they're hosting from
2023-04-02 23:22:54 +09:00
fmt . Print ( "Please enter the country you're hosting from. " )
2023-04-02 01:53:44 +09:00
var country string
fmt . Scanln ( & country )
if country != "" {
vars . InstanceCountry = country
} else {
vars . InstanceCountry = ""
}
// ask user about their ISP/Hosting provider
2023-04-02 23:22:54 +09:00
fmt . Print ( "Please enter your ISP/Hosting provider. " )
2023-04-02 01:53:44 +09:00
var isp string
fmt . Scanln ( & isp )
if isp != "" {
vars . InstanceProvider = isp
} else {
vars . InstanceProvider = ""
}
// ask user about Cloudflare status
2023-04-02 23:22:54 +09:00
fmt . Print ( "Are you using Cloudflare? (y/n) " )
2023-04-02 01:53:44 +09:00
var cloudflare string
fmt . Scanln ( & cloudflare )
if cloudflare == "y" {
vars . CloudflareStatus = "true"
} else {
vars . CloudflareStatus = "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
2023-04-02 23:22:54 +09:00
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" )
2023-04-02 01:53:44 +09:00
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 )
println ( "All done! You can now start your GotHub instance with 'gothub serve'." )
println ( "You can review the environment variables with 'gothub env'." )
}