akester/go-shell

Fork Project
[550d2ec7c] Breaking apart files functions where it makes sense
/*
 * go-shell - Storehouse jailed shell program
 *
 * For full license and copyright information, see the README and COPYING file.
 */

package main

import (
	"log"
	"flag"
	"fmt"
	"os"
	
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

func main() {
	/* Load CLI flags */
	var configFileLocation = flag.String("config", "parameters.yml", "parameters.yml file location")
	var debug = flag.Bool("debug", false, "Enable more verbost output")
	var username = flag.String("user", "", "username to open shell for")
	flag.Parse()
	
	l := log.New(os.Stderr, "", 0)
	if *username == "" {
		l.Fatal("No user specified.")
	}
	
	config, err := loadConfigFile(*configFileLocation)
	if err != nil {
		l.Fatal(err)
	}
	
	connectStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", config.DatabaseUser, config.DatabasePassword, config.DatabaseHost, config.DatabasePort, config.DatabaseName)
	db, err := gorm.Open("mysql", connectStr)
	defer db.Close()
	if err != nil {
		if *debug {
			l.Print (fmt.Sprintf("DB Connect error: %s", err))
		}
		
		os.Exit(1)
	}
	if *debug {
		db.LogMode(true)
	}
	
	/* Load our user object */
	user, err := getUser(username, db)
	if err != nil  {
		if *debug {
			l.Print (fmt.Sprintf("Couldn't load user: %s", err))
		}
		
		os.Exit(1)
	}
	if *debug {
		l.Print(fmt.Sprintf("Loaded user: %s", user.Username))
	}
	
	if !user.IsActive() {
		l.Fatal("Your account is not in good standing and cannot access projects. Login to your Storehouse account to see why.")
	}
}