From 5552dcbbc7f630ada7c7d030b37c2efdce750ace Mon Sep 17 00:00:00 2001
From: elcore <eldinhadzic@protonmail.com>
Date: Wed, 21 Feb 2018 18:56:09 +0100
Subject: [PATCH] startup/shutdown: Remove deprecated startup/shutdown
 directives (#2033)

* caddy: Remove deprecated startup/shutdown directives

* caddyhttp: Remove deprecated startup/shutdown directives

Users should use 'on startup' and 'on shutdown' instead.
---
 caddyhttp/caddyhttp.go                  |  1 -
 caddyhttp/caddyhttp_test.go             |  2 +-
 startupshutdown/startupshutdown.go      | 98 -------------------------
 startupshutdown/startupshutdown_test.go | 69 -----------------
 4 files changed, 1 insertion(+), 169 deletions(-)
 delete mode 100644 startupshutdown/startupshutdown.go
 delete mode 100644 startupshutdown/startupshutdown_test.go

diff --git a/caddyhttp/caddyhttp.go b/caddyhttp/caddyhttp.go
index 3c9c81ef0..d9d97e9d0 100644
--- a/caddyhttp/caddyhttp.go
+++ b/caddyhttp/caddyhttp.go
@@ -46,5 +46,4 @@ import (
 	_ "github.com/mholt/caddy/caddyhttp/timeouts"
 	_ "github.com/mholt/caddy/caddyhttp/websocket"
 	_ "github.com/mholt/caddy/onevent"
-	_ "github.com/mholt/caddy/startupshutdown"
 )
diff --git a/caddyhttp/caddyhttp_test.go b/caddyhttp/caddyhttp_test.go
index 56ec884e0..ca3b97e19 100644
--- a/caddyhttp/caddyhttp_test.go
+++ b/caddyhttp/caddyhttp_test.go
@@ -25,7 +25,7 @@ import (
 // ensure that the standard plugins are in fact plugged in
 // and registered properly; this is a quick/naive way to do it.
 func TestStandardPlugins(t *testing.T) {
-	numStandardPlugins := 33 // importing caddyhttp plugs in this many plugins
+	numStandardPlugins := 31 // importing caddyhttp plugs in this many plugins
 	s := caddy.DescribePlugins()
 	if got, want := strings.Count(s, "\n"), numStandardPlugins+5; got != want {
 		t.Errorf("Expected all standard plugins to be plugged in, got:\n%s", s)
diff --git a/startupshutdown/startupshutdown.go b/startupshutdown/startupshutdown.go
deleted file mode 100644
index 72fe9225c..000000000
--- a/startupshutdown/startupshutdown.go
+++ /dev/null
@@ -1,98 +0,0 @@
-// Copyright 2015 Light Code Labs, LLC
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package startupshutdown
-
-import (
-	"fmt"
-	"strings"
-
-	"github.com/google/uuid"
-	"github.com/mholt/caddy"
-	"github.com/mholt/caddy/onevent/hook"
-)
-
-func init() {
-	caddy.RegisterPlugin("startup", caddy.Plugin{Action: Startup})
-	caddy.RegisterPlugin("shutdown", caddy.Plugin{Action: Shutdown})
-}
-
-// Startup (an alias for 'on startup') registers a startup callback to execute during server start.
-func Startup(c *caddy.Controller) error {
-	config, err := onParse(c, caddy.InstanceStartupEvent)
-	if err != nil {
-		return c.ArgErr()
-	}
-
-	// Register Event Hooks.
-	c.OncePerServerBlock(func() error {
-		for _, cfg := range config {
-			caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook)
-		}
-		return nil
-	})
-
-	fmt.Println("NOTICE: Startup directive will be removed in a later version. Please migrate to 'on startup'")
-
-	return nil
-}
-
-// Shutdown (an alias for 'on shutdown') registers a shutdown callback to execute during server start.
-func Shutdown(c *caddy.Controller) error {
-	config, err := onParse(c, caddy.ShutdownEvent)
-	if err != nil {
-		return c.ArgErr()
-	}
-
-	// Register Event Hooks.
-	for _, cfg := range config {
-		caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook)
-	}
-
-	fmt.Println("NOTICE: Shutdown directive will be removed in a later version. Please migrate to 'on shutdown'")
-
-	return nil
-}
-
-func onParse(c *caddy.Controller, event caddy.EventName) ([]*hook.Config, error) {
-	var config []*hook.Config
-
-	for c.Next() {
-		cfg := new(hook.Config)
-
-		args := c.RemainingArgs()
-		if len(args) == 0 {
-			return config, c.ArgErr()
-		}
-
-		// Configure Event.
-		cfg.Event = event
-
-		// Assign an unique ID.
-		cfg.ID = uuid.New().String()
-
-		// Extract command and arguments.
-		command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " "))
-		if err != nil {
-			return config, c.Err(err.Error())
-		}
-
-		cfg.Command = command
-		cfg.Args = args
-
-		config = append(config, cfg)
-	}
-
-	return config, nil
-}
diff --git a/startupshutdown/startupshutdown_test.go b/startupshutdown/startupshutdown_test.go
deleted file mode 100644
index e68afd171..000000000
--- a/startupshutdown/startupshutdown_test.go
+++ /dev/null
@@ -1,69 +0,0 @@
-// Copyright 2015 Light Code Labs, LLC
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package startupshutdown
-
-import (
-	"testing"
-
-	"github.com/mholt/caddy"
-)
-
-func TestStartup(t *testing.T) {
-	tests := []struct {
-		name      string
-		input     string
-		shouldErr bool
-	}{
-		{name: "noInput", input: "startup", shouldErr: true},
-		{name: "startup", input: "startup cmd arg", shouldErr: false},
-	}
-
-	for _, test := range tests {
-		t.Run(test.name, func(t *testing.T) {
-			c := caddy.NewTestController("", test.input)
-
-			err := Startup(c)
-			if err == nil && test.shouldErr {
-				t.Error("Test didn't error, but it should have")
-			} else if err != nil && !test.shouldErr {
-				t.Errorf("Test errored, but it shouldn't have; got '%v'", err)
-			}
-		})
-	}
-}
-
-func TestShutdown(t *testing.T) {
-	tests := []struct {
-		name      string
-		input     string
-		shouldErr bool
-	}{
-		{name: "noInput", input: "shutdown", shouldErr: true},
-		{name: "shutdown", input: "shutdown cmd arg", shouldErr: false},
-	}
-
-	for _, test := range tests {
-		t.Run(test.name, func(t *testing.T) {
-			c := caddy.NewTestController("", test.input)
-
-			err := Shutdown(c)
-			if err == nil && test.shouldErr {
-				t.Error("Test didn't error, but it should have")
-			} else if err != nil && !test.shouldErr {
-				t.Errorf("Test errored, but it shouldn't have; got '%v'", err)
-			}
-		})
-	}
-}