Simplify, refactor and rename to bring in line with CTL

This patch does the following:
1.  Simplify the client websocket message transformation
2.  Move the cmd/airshipui to cmd
3.  move internal to pkg
4.  Clean up the copyright checker
5.  clean up the linting in the makefile

Change-Id: I1381d025e8058cbfba44b58ec3c2ec5c2aa36de5
This commit is contained in:
Schiefelbein, Andrew 2020-08-04 13:46:16 -05:00
parent a0d1b40230
commit a15a4d87e2
28 changed files with 60 additions and 147 deletions

View File

@ -43,7 +43,7 @@ COVER_PROFILE ?= cover.out
COVER_EXCLUDE ?= (zz_generated) COVER_EXCLUDE ?= (zz_generated)
# Override the value of the version variable in main.go # Override the value of the version variable in main.go
LD_FLAGS= '-X opendev.org/airship/airshipui/internal/commands.version=$(GIT_VERSION)' LD_FLAGS= '-X opendev.org/airship/airshipui/pkg/commands.version=$(GIT_VERSION)'
GO_FLAGS := -ldflags=$(LD_FLAGS) GO_FLAGS := -ldflags=$(LD_FLAGS)
BUILD_DIR := bin BUILD_DIR := bin
@ -72,7 +72,7 @@ $(MAIN): FORCE
@mkdir -p $(BUILD_DIR) @mkdir -p $(BUILD_DIR)
cd $(WEBDIR) && (PATH="$(PATH):$(NODEJS_BIN)"; $(NPM) install) && cd .. cd $(WEBDIR) && (PATH="$(PATH):$(NODEJS_BIN)"; $(NPM) install) && cd ..
cd $(WEBDIR) && (PATH="$(PATH):$(NODEJS_BIN)"; $(NG) build) && cd .. cd $(WEBDIR) && (PATH="$(PATH):$(NODEJS_BIN)"; $(NG) build) && cd ..
go build -o $(MAIN)$(EXTENSION) $(GO_FLAGS) cmd/$(@F)/main.go go build -o $(MAIN)$(EXTENSION) $(GO_FLAGS) cmd/main.go
FORCE: FORCE:
@ -180,10 +180,11 @@ lint: tidy $(LINTER)
@./tools/whitespace_linter @./tools/whitespace_linter
@echo "Running golangci-lint linting step..." @echo "Running golangci-lint linting step..."
$(LINTER) run --config $(LINTER_CONFIG) $(LINTER) run --config $(LINTER_CONFIG)
@echo "Installing NPM & running client linting step..." # TODO: Replace eslint with TS lint
./tools/install_npm # @echo "Installing NPM & running client linting step..."
cd $(WEBDIR) && (PATH="$(PATH):$(NODEJS_BIN)"; $(NPM) install) && cd .. # ./tools/install_npm
cd $(WEBDIR) && (PATH="$(PATH):$(NODEJS_BIN)"; $(NG) build) && cd .. # cd $(WEBDIR) && (PATH="$(PATH):$(NODEJS_BIN)"; $(NPM) install) && cd ..
# cd $(WEBDIR) && (PATH="$(PATH):$(NODEJS_BIN)"; $(NG) build) && cd ..
@echo "Linting completed successfully" @echo "Linting completed successfully"
.PHONY: tidy .PHONY: tidy

View File

@ -13,80 +13,12 @@ export class WebsocketService {
private ws: WebSocket; private ws: WebSocket;
private timeout: number; private timeout: number;
private static convertIncomingMessageJsonToObject(incomingMessage: string): WebsocketMessage { private static messageToObject(incomingMessage: string): WebsocketMessage {
const json = JSON.parse(incomingMessage); let json = JSON.parse(incomingMessage);
const messageTransform = new WebsocketMessage(); let obj = new WebsocketMessage();
if (typeof json.type === 'string') {
messageTransform.type = json.type; Object.assign(obj, json);
} return obj;
if (typeof json.component === 'string') {
messageTransform.component = json.component;
}
if (typeof json.subComponent === 'string') {
messageTransform.subComponent = json.subComponent;
}
if (typeof json.timestamp === 'number') {
messageTransform.timestamp = json.timestamp;
}
if (typeof json.error === 'string') {
messageTransform.error = json.error;
}
if (typeof json.fade === 'boolean') {
messageTransform.fade = json.fade;
}
if (typeof json.html === 'string') {
messageTransform.html = json.html;
}
if (typeof json.isAuthenticated === 'boolean') {
messageTransform.isAuthenticated = json.isAuthenticated;
}
if (typeof json.message === 'string') {
messageTransform.message = json.message;
}
if (typeof json.data === 'string' && JSON.parse(json.data)) {
messageTransform.data = JSON.parse(json.data);
}
if (typeof json.yaml === 'string') {
messageTransform.yaml = json.yaml;
}
if (typeof json.dashboards !== undefined && Array.isArray(json.dashboards)) {
json.dashboards.forEach(dashboard => {
const dashboardTransform = new Dashboard();
if (typeof dashboard.name === 'string') {
dashboardTransform.name = dashboard.name;
}
if (typeof dashboard.baseURL === 'string') {
dashboardTransform.baseURL = dashboard.baseURL;
}
if (typeof dashboard.path === 'string') {
dashboardTransform.path = dashboard.path;
}
if (typeof dashboard.isProxied === 'boolean') {
dashboardTransform.isProxied = dashboard.isProxied;
}
if (typeof dashboard.executable === 'object') {
const executableTransform = new Executable();
if (typeof dashboard.executable.autoStart === 'boolean') {
executableTransform.autoStart = dashboard.executable.autoStart;
}
if (typeof dashboard.executable.filePath === 'string') {
executableTransform.filePath = dashboard.executable.filePath;
}
if (typeof dashboard.executable.args !== undefined && Array.isArray(typeof dashboard.executable.args)) {
dashboard.executable.args.forEach(arg => {
if (typeof arg === 'string') {
executableTransform.args.push(arg);
}
});
}
}
if (messageTransform.dashboards === undefined) {
messageTransform.dashboards = [];
}
messageTransform.dashboards.push(dashboardTransform);
});
}
return messageTransform;
} }
constructor() { constructor() {
@ -107,7 +39,7 @@ export class WebsocketService {
this.ws = new WebSocket('ws://localhost:8080/ws'); this.ws = new WebSocket('ws://localhost:8080/ws');
this.ws.onmessage = (event) => { this.ws.onmessage = (event) => {
this.subject.next(WebsocketService.convertIncomingMessageJsonToObject(event.data)); this.subject.next(WebsocketService.messageToObject(event.data));
}; };
this.ws.onerror = (event) => { this.ws.onerror = (event) => {

2
cmd/airshipui/main.go → cmd/main.go Normal file → Executable file
View File

@ -15,7 +15,7 @@
package main package main
import ( import (
"opendev.org/airship/airshipui/internal/commands" "opendev.org/airship/airshipui/pkg/commands"
) )
func main() { func main() {

View File

@ -1,44 +0,0 @@
/*
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
https://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 webservice
import (
"github.com/pkg/errors"
"net/http"
"opendev.org/airship/airshipui/util/utilfile"
"opendev.org/airship/airshipui/util/utilhttp"
)
const (
clientPath = "client/dist/airshipui-ui"
)
func serveFile(w http.ResponseWriter, r *http.Request) {
filePath, filePathErr := utilfile.FilePath(clientPath, r.URL.Path)
if filePathErr != nil {
utilhttp.HandleErr(w, errors.WithStack(filePathErr), http.StatusInternalServerError)
return
}
fileExists, fileExistsErr := utilfile.Exists(filePath)
if fileExistsErr != nil {
utilhttp.HandleErr(w, errors.WithStack(fileExistsErr), http.StatusInternalServerError)
return
}
if fileExists {
http.ServeFile(w, r, filePath)
}
}

View File

@ -25,8 +25,8 @@ import (
"sync" "sync"
"syscall" "syscall"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
"opendev.org/airship/airshipui/internal/webservice" "opendev.org/airship/airshipui/pkg/webservice"
) )
// ProcessGrpCmd wraps an exec.Cmd and a signal chan // ProcessGrpCmd wraps an exec.Cmd and a signal chan

View File

@ -24,8 +24,8 @@ import (
"unsafe" "unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
"opendev.org/airship/airshipui/internal/webservice" "opendev.org/airship/airshipui/pkg/webservice"
) )
// struct to store PID and process handle // struct to store PID and process handle

View File

@ -26,8 +26,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
"opendev.org/airship/airshipui/internal/webservice" "opendev.org/airship/airshipui/pkg/webservice"
) )
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands

View File

@ -22,7 +22,7 @@ import (
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/version" "opendev.org/airship/airshipctl/pkg/version"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
) )
// obtain base path of caller so references to html // obtain base path of caller so references to html

View File

@ -18,7 +18,7 @@ import (
"fmt" "fmt"
"opendev.org/airship/airshipctl/pkg/bootstrap/isogen" "opendev.org/airship/airshipctl/pkg/bootstrap/isogen"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
) )
// HandleBaremetalRequest will flop between requests so we don't have to have them all mapped as function calls // HandleBaremetalRequest will flop between requests so we don't have to have them all mapped as function calls

View File

@ -18,7 +18,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
"opendev.org/airship/airshipui/util/utiltest" "opendev.org/airship/airshipui/util/utiltest"
) )

View File

@ -21,7 +21,7 @@ import (
"os" "os"
"opendev.org/airship/airshipctl/pkg/document/pull" "opendev.org/airship/airshipctl/pkg/document/pull"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
) )
// HandleDocumentRequest will flop between requests so we don't have to have them all mapped as function calls // HandleDocumentRequest will flop between requests so we don't have to have them all mapped as function calls

View File

@ -18,7 +18,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
"opendev.org/airship/airshipui/util/utiltest" "opendev.org/airship/airshipui/util/utiltest"
) )

View File

@ -17,7 +17,7 @@ package webservice
import ( import (
"time" "time"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
) )
// Alerts serves as a queue to hold alerts to be sent to the UI, // Alerts serves as a queue to hold alerts to be sent to the UI,

View File

@ -19,7 +19,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
) )
func TestSendAlert(t *testing.T) { func TestSendAlert(t *testing.T) {

View File

@ -21,7 +21,7 @@ import (
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
) )
// map of proxy targets which will be used based on the request // map of proxy targets which will be used based on the request

View File

@ -19,12 +19,36 @@ import (
"net/http" "net/http"
"time" "time"
"opendev.org/airship/airshipui/internal/configs" "github.com/pkg/errors"
"opendev.org/airship/airshipui/pkg/configs"
"opendev.org/airship/airshipui/util/utilfile"
"opendev.org/airship/airshipui/util/utilhttp"
) )
// semaphore to signal the UI to authenticate // semaphore to signal the UI to authenticate
var isAuthenticated bool var isAuthenticated bool
const (
clientPath = "client/dist/airshipui-ui"
)
// test if path and file exists, if it does send a page, else 404 for you
func serveFile(w http.ResponseWriter, r *http.Request) {
filePath, filePathErr := utilfile.FilePath(clientPath, r.URL.Path)
if filePathErr != nil {
utilhttp.HandleErr(w, errors.WithStack(filePathErr), http.StatusInternalServerError)
return
}
fileExists, fileExistsErr := utilfile.Exists(filePath)
if fileExistsErr != nil {
utilhttp.HandleErr(w, errors.WithStack(fileExistsErr), http.StatusInternalServerError)
return
}
if fileExists {
http.ServeFile(w, r, filePath)
}
}
// handle an auth complete attempt // handle an auth complete attempt
func handleAuth(http.ResponseWriter, *http.Request) { func handleAuth(http.ResponseWriter, *http.Request) {
// TODO: handle the response body to capture the credentials // TODO: handle the response body to capture the credentials

View File

@ -20,7 +20,7 @@ import (
"testing" "testing"
"time" "time"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"

View File

@ -21,8 +21,8 @@ import (
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
"opendev.org/airship/airshipui/internal/integrations/ctl" "opendev.org/airship/airshipui/pkg/ctl"
) )
// gorilla ws specific HTTP upgrade to WebSockets // gorilla ws specific HTTP upgrade to WebSockets

View File

@ -23,7 +23,7 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
) )
func TestClientInit(t *testing.T) { func TestClientInit(t *testing.T) {

View File

@ -18,7 +18,7 @@ declare FILES_MISSING_COPYRIGHT=()
check_license() { check_license() {
ext=$1 ext=$1
# skipping license for testdata and manifests folders # skipping license for testdata and manifests folders
FILES=$(find -L . -name "*.${ext}" | grep -v "testdata" | grep -v "manifests") FILES=$(find -L . -name "*.${ext}" | grep -v "testdata" | grep -v "manifests" | grep -v "tools/*node*" | grep -v "client/node_modules" | grep -v "client/dist")
for each in $FILES for each in $FILES
do do

View File

@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipui/internal/configs" "opendev.org/airship/airshipui/pkg/configs"
) )
// TODO: Determine if this should be broken out into it's own file // TODO: Determine if this should be broken out into it's own file