Skip to content

Commit

Permalink
removed exports - cleaned - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlong committed Oct 11, 2018
1 parent 5c51ba0 commit b545143
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 322 deletions.
172 changes: 18 additions & 154 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (
"time"
)

// CatchCLI will run functions based on the commands sent to Statup
func CatchCLI(args []string) error {
// catchCLI will run functions based on the commands sent to Statup
func catchCLI(args []string) error {
dir := utils.Directory
utils.InitLogs()
source.Assets()
LoadDotEnvs()
loadDotEnvs()

switch args[0] {
case "app":
Expand All @@ -55,15 +55,13 @@ func CatchCLI(args []string) error {
return errors.New("end")
}
case "sass":
utils.InitLogs()
source.Assets()
err := source.CompileSASS(dir)
if err == nil {
return errors.New("end")
}
return err
case "update":
gitCurrent, err := CheckGithubUpdates()
gitCurrent, err := checkGithubUpdates()
if err != nil {
return nil
}
Expand All @@ -87,7 +85,7 @@ func CatchCLI(args []string) error {
case "export":
var err error
fmt.Printf("Statup v%v Exporting Static 'index.html' page...\n", VERSION)
core.Configs, err = core.LoadConfig(dir)
core.Configs, err = core.LoadConfigFile(dir)
if err != nil {
utils.Log(4, "config.yml file not found")
return err
Expand Down Expand Up @@ -126,7 +124,7 @@ func CatchCLI(args []string) error {
// RunOnce will initialize the Statup application and check each service 1 time, will not run HTTP server
func RunOnce() {
var err error
core.Configs, err = core.LoadConfig(utils.Directory)
core.Configs, err = core.LoadConfigFile(utils.Directory)
if err != nil {
utils.Log(4, "config.yml file not found")
}
Expand All @@ -138,7 +136,7 @@ func RunOnce() {
if err != nil {
fmt.Println("Core database was not found, Statup is not setup yet.")
}
core.CoreApp.SelectAllServices()
_, err = core.CoreApp.SelectAllServices(true)
if err != nil {
utils.Log(4, err)
}
Expand Down Expand Up @@ -170,156 +168,22 @@ func HelpEcho() {
fmt.Println("Give Statup a Star at https://github.com/hunterlong/statup")
}

//
//func TestPlugin(plug types.PluginActions) {
// defer utils.DeleteFile("./.plugin_test.db")
// source.Assets()
//
// info := plug.GetInfo()
// fmt.Printf("\n" + BRAKER + "\n")
// fmt.Printf(" Plugin Name: %v\n", info.Name)
// fmt.Printf(" Plugin Description: %v\n", info.Description)
// fmt.Printf(" Plugin Routes: %v\n", len(plug.Routes()))
// for k, r := range plug.Routes() {
// fmt.Printf(" - Route %v - (%v) /%v \n", k+1, r.Method, r.URL)
// }
//
// // Function to create a new Core with example services, hits, failures, users, and default communications
// FakeSeed(plug)
//
// fmt.Println("\n" + BRAKER)
// fmt.Println(POINT + "Sending 'OnLoad(sqlbuilder.Database)'")
// core.OnLoad(core.DbSession)
// fmt.Println("\n" + BRAKER)
// fmt.Println(POINT + "Sending 'OnSuccess(Service)'")
// core.OnSuccess(core.SelectService(1))
// fmt.Println("\n" + BRAKER)
// fmt.Println(POINT + "Sending 'OnFailure(Service, FailureData)'")
// fakeFailD := &types.failure{
// Issue: "No issue, just testing this plugin. This would include HTTP failure information though",
// }
// core.OnFailure(core.SelectService(1), fakeFailD)
// fmt.Println("\n" + BRAKER)
// fmt.Println(POINT + "Sending 'OnSettingsSaved(Core)'")
// fmt.Println(BRAKER)
// core.OnSettingsSaved(core.CoreApp.ToCore())
// fmt.Println("\n" + BRAKER)
// fmt.Println(POINT + "Sending 'OnNewService(Service)'")
// core.OnNewService(core.SelectService(2))
// fmt.Println("\n" + BRAKER)
// fmt.Println(POINT + "Sending 'OnNewUser(user)'")
// user, _ := core.SelectUser(1)
// core.OnNewUser(user)
// fmt.Println("\n" + BRAKER)
// fmt.Println(POINT + "Sending 'OnUpdateService(Service)'")
// srv := core.SelectService(2)
// srv.Type = "http"
// srv.Domain = "https://yahoo.com"
// core.OnUpdateService(srv)
// fmt.Println("\n" + BRAKER)
// fmt.Println(POINT + "Sending 'OnDeletedService(Service)'")
// core.OnDeletedService(core.SelectService(1))
// fmt.Println("\n" + BRAKER)
//}
//
//func FakeSeed(plug types.PluginActions) {
// var err error
// core.CoreApp = core.NewCore()
//
// core.CoreApp.AllPlugins = []types.PluginActions{plug}
//
// fmt.Printf("\n" + BRAKER)
//
// fmt.Println("\nCreating a SQLite database for testing, will be deleted automatically...")
// core.DbSession, err = gorm.Open("sqlite", "./.plugin_test.db")
// if err != nil {
// utils.Log(3, err)
// }
//
// fmt.Println("Finished creating Test SQLite database")
// fmt.Println("Inserting example services into test database...")
//
// core.CoreApp.Name = "Plugin Test"
// core.CoreApp.Description = "This is a fake Core for testing your plugin"
// core.CoreApp.Domain = "http://localhost:8080"
// core.CoreApp.ApiSecret = "0x0x0x0x0"
// core.CoreApp.ApiKey = "abcdefg12345"
//
// fakeSrv := &core.Service{Service: &types.Service{
// Name: "Test Plugin Service",
// Domain: "https://google.com",
// Method: "GET",
// }}
// fakeSrv.Create()
//
// fakeSrv2 := &core.Service{Service: &types.Service{
// Name: "Awesome Plugin Service",
// Domain: "https://netflix.com",
// Method: "GET",
// }}
// fakeSrv2.Create()
//
// fakeUser := &types.user{
// Id: 6334,
// Username: "Bulbasaur",
// Password: "$2a$14$NzT/fLdE3f9iB1Eux2C84O6ZoPhI4NfY0Ke32qllCFo8pMTkUPZzy",
// Email: "[email protected]",
// Admin: true,
// CreatedAt: time.Now(),
// }
// fakeUser.Create()
//
// fakeUser = &types.user{
// Id: 6335,
// Username: "Billy",
// Password: "$2a$14$NzT/fLdE3f9iB1Eux2C84O6ZoPhI4NfY0Ke32qllCFo8pMTkUPZzy",
// Email: "[email protected]",
// CreatedAt: time.Now(),
// }
// fakeUser.Create()
//
// for i := 0; i <= 50; i++ {
// dd := &types.Hit{
// Latency: rand.Float64(),
// }
// fakeSrv.CreateHit(dd)
//
// dd = &types.Hit{
// Latency: rand.Float64(),
// }
// fakeSrv2.CreateHit(dd)
//
// fail := &types.failure{
// Issue: "This is not an issue, but it would container HTTP response errors.",
// }
// fakeSrv.CreateFailure(fail)
//
// fail = &types.failure{
// Issue: "HTTP Status Code 521 did not match 200",
// }
// fakeSrv.CreateFailure(fail)
// }
//
// fmt.Println("Seeding example data is complete, running Plugin Tests")
//
//}

func CheckGithubUpdates() (GithubResponse, error) {
var gitResp GithubResponse
func checkGithubUpdates() (githubResponse, error) {
var gitResp githubResponse
response, err := http.Get("https://api.github.com/repos/hunterlong/statup/releases/latest")
if err != nil {
return GithubResponse{}, err
return githubResponse{}, err
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return GithubResponse{}, err
return githubResponse{}, err
}
err = json.Unmarshal(contents, &gitResp)
return gitResp, err
}

type GithubResponse struct {
type githubResponse struct {
URL string `json:"url"`
AssetsURL string `json:"assets_url"`
UploadURL string `json:"upload_url"`
Expand All @@ -330,17 +194,17 @@ type GithubResponse struct {
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Author GitAuthor `json:"author"`
Author gitAuthor `json:"author"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Assets []GitAssets `json:"assets"`
Assets []gitAssets `json:"assets"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Body string `json:"body"`
}

type GitAuthor struct {
type gitAuthor struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
Expand All @@ -361,13 +225,13 @@ type GitAuthor struct {
SiteAdmin bool `json:"site_admin"`
}

type GitAssets struct {
type gitAssets struct {
URL string `json:"url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Label string `json:"label"`
Uploader GitUploader `json:"uploader"`
Uploader gitUploader `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int `json:"size"`
Expand All @@ -377,7 +241,7 @@ type GitAssets struct {
BrowserDownloadURL string `json:"browser_download_url"`
}

type GitUploader struct {
type gitUploader struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
Expand Down
18 changes: 9 additions & 9 deletions cmd/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

func TestRunSQLiteApp(t *testing.T) {
t.SkipNow()
run := CatchCLI([]string{"app"})
run := catchCLI([]string{"app"})
assert.Nil(t, run)
}

Expand Down Expand Up @@ -64,46 +64,46 @@ func TestAssetsCommand(t *testing.T) {
}

func TestVersionCLI(t *testing.T) {
run := CatchCLI([]string{"version"})
run := catchCLI([]string{"version"})
assert.EqualError(t, run, "end")
}

func TestAssetsCLI(t *testing.T) {
run := CatchCLI([]string{"assets"})
run := catchCLI([]string{"assets"})
assert.EqualError(t, run, "end")
assert.FileExists(t, dir+"/assets/css/base.css")
assert.FileExists(t, dir+"/assets/scss/base.scss")
}

func TestSassCLI(t *testing.T) {
run := CatchCLI([]string{"sass"})
run := catchCLI([]string{"sass"})
assert.EqualError(t, run, "end")
assert.FileExists(t, dir+"/assets/css/base.css")
}

func TestUpdateCLI(t *testing.T) {
t.SkipNow()
run := CatchCLI([]string{"update"})
run := catchCLI([]string{"update"})
assert.EqualError(t, run, "end")
}

func TestTestPackageCLI(t *testing.T) {
run := CatchCLI([]string{"test", "plugins"})
run := catchCLI([]string{"test", "plugins"})
assert.EqualError(t, run, "end")
}

func TestHelpCLI(t *testing.T) {
run := CatchCLI([]string{"help"})
run := catchCLI([]string{"help"})
assert.EqualError(t, run, "end")
}

func TestRunOnceCLI(t *testing.T) {
t.SkipNow()
run := CatchCLI([]string{"run"})
run := catchCLI([]string{"run"})
assert.Nil(t, run)
}

func TestEnvCLI(t *testing.T) {
run := CatchCLI([]string{"env"})
run := catchCLI([]string{"env"})
assert.Error(t, run)
}
14 changes: 5 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func parseFlags() {
func main() {
var err error
parseFlags()
LoadDotEnvs()
loadDotEnvs()
source.Assets()
utils.InitLogs()
args := flag.Args()

if len(args) >= 1 {
err := CatchCLI(args)
err := catchCLI(args)
if err != nil {
if err.Error() == "end" {
os.Exit(0)
Expand All @@ -73,7 +73,7 @@ func main() {
}
}
utils.Log(1, fmt.Sprintf("Starting Statup v%v", VERSION))
core.Configs, err = core.LoadConfig(utils.Directory)
core.Configs, err = core.LoadConfigFile(utils.Directory)
if err != nil {
utils.Log(3, err)
core.SetupMode = true
Expand All @@ -84,8 +84,8 @@ func main() {
mainProcess()
}

// LoadDotEnvs attempts to load database configs from a '.env' file in root directory
func LoadDotEnvs() error {
// loadDotEnvs attempts to load database configs from a '.env' file in root directory
func loadDotEnvs() error {
err := godotenv.Load()
if err == nil {
utils.Log(1, "Environment file '.env' Loaded")
Expand All @@ -98,10 +98,6 @@ func LoadDotEnvs() error {
func mainProcess() {
dir := utils.Directory
var err error
core.Configs, err = core.LoadConfig(dir)
if err != nil {
utils.Log(4, fmt.Sprintf("could not load config.yml %v", err))
}
err = core.Configs.Connect(false, dir)
if err != nil {
utils.Log(4, fmt.Sprintf("could not connect to database: %v", err))
Expand Down
Loading

0 comments on commit b545143

Please sign in to comment.