#
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
#   Red Hat, Inc. - initial API and implementation

apiVersion: 1.0.0
metadata:
  name: che-machine-exec
projects:
  - name: che-machine-exec
    source:
      type: git
      location: https://github.com/eclipse-che/che-machine-exec.git
components:
  - alias: ms-vscode.go
    id: golang/go/latest
    type: chePlugin
    memoryLimit: 1G

  - alias: typescript-plugin
    type: chePlugin
    id: vscode/typescript-language-features/latest
    memoryLimit: 512M

  - alias: theia-dev-or-cloud-shell
    type: dockerimage
    image: quay.io/eclipse/che-theia-dev:next
    mountSources: true
    endpoints:
      - name: theia-dev-or-cloud-shell
        port: 3130
        attributes:
          protocol: http
          secure: 'false'
          public: 'true'
    memoryLimit: 512M

  - alias: dev
    type: dockerimage
    image: quay.io/eclipse/che-golang-1.14:next
    mountSources: true
    endpoints:
      - name: exec-server
        port: 5555
        attributes:
          type: terminal-dev
          protocol: ws
          secure: 'false'
          public: 'true'
    memoryLimit: 1024M
    env:
      - name: GOPATH
        value: /go:$(CHE_PROJECTS_ROOT)
      - name: GOCACHE
      # replicate the GOCACHE from the plugin, even though the cache is not shared
      # between the two
        value: /tmp/.cache

commands:
  - name: compile
    actions:
      - type: exec
        component: dev
        command: ./compile.sh
        workdir: /projects/che-machine-exec

  - name: test
    actions:
      - type: exec
        component: dev
        command: export CHE_WORKSPACE_ID=test_id; go test ./...
        workdir: /projects/che-machine-exec

  - name: format
    actions:
      - type: exec
        component: dev
        command: go fmt ./... && printf "\033[32mDone.\033[0m"
        workdir: /projects/che-machine-exec

  - name: start exec server
    actions:
      - type: exec
        component: dev
        command: ./che-machine-exec --url 0.0.0.0:5555
        workdir: /projects/che-machine-exec

  - name: stop exec server
    actions:
      - type: exec
        component: dev
        command: >-
          pid=$(pidof che-machine-exec) && kill ${pid} &&
          printf "\033[32mDone.\033[0m" ||
          printf "\033[32mNothing to stop... Done.\033[0m"

  - name: build cloud shell
    actions:
      - type: exec
        command: yarn && yarn run build
        component: theia-dev-or-cloud-shell
        workdir: /projects/che-machine-exec/cloud-shell

  - name: start cloud shell
    actions:
      - workdir: /projects/che-machine-exec
        type: exec
        command: >-
          ./che-machine-exec --url 0.0.0.0:3130 --static /projects/che-machine-exec/cloud-shell
        component: dev

  - name: Debug exec server
    actions:
      - type: vscode-launch
        referenceContent: |
          {
            "version": "0.2.0",
            "configurations": [
              {
                "useApiV1": false,
                "dlvLoadConfig": {
                  "followPointers": true,
                  "maxVariableRecurse": 1,
                  "maxStringLen": 3000,
                  "maxArrayValues": 100,
                  "maxStructFields": -1
                },
                "name": "Remote debug che-machine-exec",
                "type": "go",
                "request": "launch",
                "mode": "auto",
                "program": "${workspaceFolder}/che-machine-exec/main.go",
                "cwd": "${workspaceFolder}",
                "args": [ "--url", "0.0.0.0:3130", "--static", "/projects/che-machine-exec/cloud-shell" ]
              }
            ]
          }
