示例#1
0
文件: move.go 项目: klauern/caddyext
func MoveExtension(cmd *cobra.Command, args []string) {
	if len(args) < 2 {
		cmdError(cmd, ErrMissingArguments)
	}

	dir, err := directives.NewFrom(filepath.Join(caddyPath, "caddy/directives.go"))
	if err != nil {
		cmdError(cmd, err)
	}

	name := args[0]
	index, _ := strconv.Atoi(args[1])

	err = dir.MoveDirective(name, index)
	if err != nil {
		cmdError(cmd, err)
	}

	err = dir.Save()
	if err != nil {
		cmdError(cmd, err)
	}

	fmt.Println(name, "successfully moved.")
}
示例#2
0
文件: reset.go 项目: klauern/caddyext
func ResetCaddy(cmd *cobra.Command, args []string) {
	dir, err := directives.NewFrom(filepath.Join(caddyPath, "caddy/directives.go"))
	if err != nil {
		cmdError(cmd, err)
	}

	dir.Reset()

	err = dir.Save()
	if err != nil {
		cmdError(cmd, err)
	}

	fmt.Println("Active caddy state has been reseted.")
}
示例#3
0
文件: stack.go 项目: klauern/caddyext
func StackExtension(cmd *cobra.Command, args []string) {
	dir, _ := directives.NewFrom(filepath.Join(caddyPath, "caddy/directives.go"))
	list := dir.List()
	fmt.Println("\nAvailable Caddy directives/extensions:")
	fmt.Println("   (✓) ENABLED | (-) DISABLED\n")
	for i, d := range list {
		active := "✓"
		isCore := ""
		if !d.Active {
			active = "-"
		}
		if d.Core {
			isCore = "(core)"
		}
		fmt.Printf("   %d. (%s) %s %s\n", i, active, d.Name, isCore)
	}
	fmt.Println("")
}
示例#4
0
func EnableExtension(cmd *cobra.Command, args []string) {
	if len(args) < 1 {
		cmdError(cmd, ErrMissingArguments)
	}

	dir, err := directives.NewFrom(filepath.Join(caddyPath, "caddy/directives.go"))
	if err != nil {
		cmdError(cmd, err)
	}

	name := args[0]

	err = dir.EnableDirective(name)
	if err != nil {
		cmdError(cmd, err)
	}

	err = dir.Save()
	if err != nil {
		cmdError(cmd, err)
	}

	fmt.Println(name, "successfully enabled.")
}
示例#5
0
func InstallExtension(cmd *cobra.Command, args []string) {
	if len(args) < 1 {
		cmdError(cmd, ErrMissingArguments)
	}

	dir, err := directives.NewFrom(filepath.Join(caddyPath, "caddy/directives.go"))
	if err != nil {
		cmdError(cmd, err)
	}

	directives := dir.List()

	for _, directive := range args {
		dirparts := strings.Split(directive, ":")
		name := dirparts[0]

		var source string
		if len(dirparts) < 2 {
			fmt.Printf("trying to resolve `%s` from Caddy's registry\n", name)
			source = resolveExtension(name)
			if len(source) == 0 {
				cmdError(cmd, ErrInstallExtensionResolve)
			}
		} else {
			source = dirparts[1]
		}

		getExtension(source, flagUpdate)

		gopaths := strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator))
		found := false
		for _, gopath := range gopaths {
			gopath = filepath.Join(gopath, "src")
			fpath := filepath.Join(gopath, source)
			if _, err := os.Stat(fpath); err == nil {
				found = true
				break
			}
		}

		if found == false {
			cmdError(cmd, ErrInstallSourceNotFound)
		}

		err = dir.AddDirective(name, source)
		if err != nil {
			cmdError(cmd, err)
		}

		if len(flagAfter) > 0 || len(flagBefore) > 0 {
			for i, d := range directives {
				if d.Name == flagBefore {
					dir.MoveDirective(name, i)
				} else if d.Name == flagAfter {
					dir.MoveDirective(name, i+1)
				}
			}
		}

		fmt.Printf("`%s` added to Caddy.\n", name)
	}

	err = dir.Save()
	if err != nil {
		cmdError(cmd, err)
	}
}
示例#6
0
// TestAddDirectivesAfterSave tests the behavior of adding a directives to Caddy after its directives
// have been modified.
func TestAddDirectivesAfterSave(t *testing.T) {
	tmpDirectives(func(file string, err error) {
		if err != nil {
			t.Fatal(err)
		}

		// Create directives from file
		m, err := directives.NewFrom(file)
		if err != nil {
			t.Fatalf("Unexpected error creating directives manager:", err)
		}

		// Add directive, save and reload
		m.AddDirective("directive1", "github.com/mikepulaski/directive1")
		m.Save()

		m, err = directives.NewFrom(file)
		if err != nil {
			t.Fatalf("Unexpected error reloading directives manager:", err)
		}

		// Add directive, save and reload
		m.AddDirective("directive2", "github.com/mikepulaski/directive2")
		m.Save()

		m, err = directives.NewFrom(file)
		if err != nil {
			t.Fatalf("Unexpected error reloading directives manager:", err)
		}

		// Check directives
		if n := len(m.List()); n == 0 {
			t.Errorf("Expected two directives; have none")
		} else if n > 2 {
			t.Errorf("Expected two directives; have %d: %s", n, m.List())
		} else {
			directives := m.List()
			if actual, expected := directives[0].Name, "directive1"; actual != expected {
				t.Errorf("Unexpected Directive.Name: %s != %s", actual, expected)
			}
			if actual, expected := directives[1].Name, "directive2"; actual != expected {
				t.Errorf("Unexpected Directive.Name: %s != %s", actual, expected)
			}
		}

		// Check actual import paths
		imports, err := importPaths(file)
		if err != nil {
			t.Fatalf("Error getting import paths for output: %s", err)
		}

		directiveImports := []string{}
		for _, path := range imports {
			if strings.HasPrefix(path, "github.com/mholt/caddy") {
				continue
			}

			directiveImports = append(directiveImports, path)
		}

		if n := len(directiveImports); n == 0 {
			t.Errorf("Expected two directive import paths; have none")
		} else if n > 2 {
			t.Errorf("Expected two directive import paths; have %d: %s", n, directiveImports)
		} else {
			// Imports are prepended; check imports in reverse order
			if actual, expected := directiveImports[0], "github.com/mikepulaski/directive2"; actual != expected {
				t.Errorf("Unexpected import path: %s != %s", actual, expected)
			}
			if actual, expected := directiveImports[1], "github.com/mikepulaski/directive1"; actual != expected {
				t.Errorf("Unexpected import path: %s != %s", actual, expected)
			}
		}
	})
}