-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockerdetect.go
More file actions
60 lines (49 loc) · 1.56 KB
/
dockerdetect.go
File metadata and controls
60 lines (49 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package dockerdetect
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"os/exec"
"runtime"
)
type dockerResponse struct {
Installed bool
InstallationURL string
InstallationInstruction string
}
// InitAPI creates the docker detection API endpoint
func InitAPI() {
router := mux.NewRouter()
router.HandleFunc("/dockerdetect", checkDocker).Methods("GET")
log.Fatal(http.ListenAndServe("localhost:8000", router))
}
func checkDocker(w http.ResponseWriter, r *http.Request) {
var response dockerResponse
if dockerIsNotInstalled() {
if runtime.GOOS == "windows" {
response = dockerResponse{Installed: false, InstallationURL: "https://hub.docker.com/editions/community/docker-ce-desktop-windows"}
} else if runtime.GOOS == "darwin" {
response = dockerResponse{Installed: false, InstallationURL: "https://hub.docker.com/editions/community/docker-ce-desktop-mac"}
} else {
response = dockerResponse{Installed: false, InstallationURL: "https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-docker-ce"}
}
} else {
response = dockerResponse{Installed: true, InstallationInstruction: "Docker is already installed."}
}
enableCors(&w)
json.NewEncoder(w).Encode(response)
}
func dockerIsNotInstalled() bool {
out, err := exec.Command("docker", "version").Output()
if out != nil && err == nil {
fmt.Println("Docker is installed.")
return false
}
fmt.Println("Docker is not installed.")
return true
}
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}